SQLServer local and global variables introduced 05-29 Study Notes

 

variable

Data stored in memory can change the amount called variables. To store information in the memory, the user must specify the means for storing information, and name for the memory cell,

In order to facilitate access to information, which is the function of the variable. Transact-SQL can use two variables, one is a local variable, the other is a global variable.

The main difference between the local and global variables that the data stored in the account is not the same scope.

 

1, local variables

Local variables are variables that can be user-defined, its scope is only within the program. The name of the local variable is user-defined,

Named local variable names to conform SQLserver2012 identifier naming rules, local variable names must begin with @.

1.1 declare local variables

grammar:

DECLARE
{
@varaible_name datatype [,...n]
}

Parameter Description:

@varaible_name: variable name of local variables

datatype: local variable data type used, the system may be all data types and user-defined data types except text, ntext, image type.

For example, you declare a variable @testname

SQL statement is as follows:

declare  @testname  nvarchar ( 10 )

 

1.2, for the local variable

Assign values ​​to variables, there are two general ways, one is to use the select statement, is to use the set statement.

Use the select statement Assignment SQL:

use db_tse
declare @testname nvarchar(10)
select @testname=stname from Student where id='1001'
print @testname

Description: First, declare variables and student testname table inside id = stname 1001 this data is assigned to @testname, print out the final print

declare @a int
select @a=1
print @a

To multiple variable assignment:

declare @a int,@b nvarchar(10),@c int
select @a=1,@b='Tse先生',@c=2

Use set assignment statement SQL:

Simple assignment example:

declare @testname nvarchar(20)
set @testname = 'Are you ok?'
print @testname

 

2, the global variable

Global variables are internal SQLserver system pre-defined variables, without user involvement defined, the user, its scope is not limited to a program,

But any program can be called at any time. Some global variables commonly used to store the configuration settings and performance statistics SQLserver.

(1) @@ CONNECTIONS: record since the last time the server starts, all the number of connections for this server, including without success trying to connect.

(2) @@ CPU_BUSY: Record number of connections since tried since the last start, regardless of success or failure of the connection, both in ms CPU operating unit of time.

(3) @@ CURSOR_ROWS: The server returns the connection, the number of data lines open cursor removed.

(4) @@ DBTS: Returns the value of the current database, the current timestamp data type.

 

(5) @@ ERROR: an error code back to the T-SQL statement returns. If sqlserver server executes After the statement is executed successfully without error, @@ ERROR returns a value of 0.

 

 (6) @@ FETCH_STATUS: usage state returns to the last value returned by the cursor FETCH operation, and returns an integer value.

return value description
0 FETCH statement success
-1 FETCH statement failed or this line is not the result set
-2 The extracted row does not exist

 

(7) @@ IDENTITY: Returns the value of an identity column recently inserted, the return value is numeric.

 (8) @@ IDLE: return in ms SQLserver server computing time since the most recent start at a standstill state.

(9) @@ IO_BUSY: return in ms calculated SQLserver server since the last start-up time spent on inputs and outputs.

(10) @@ LOCK_TIMEOUT: Returns the current lock timeout setting data.

(11) @@ PACK_RECEIVED: sqlserver server returns the total number of data packets received from the network since the last start.

(12) @@ PACK_SENT: sqlserver server returns the total number of data packets to send over the network since the last start.

(13) @@ PROCID: Returns the ID mark currently stored procedure.

(14) @@ REMSERVER: Returns the name of the remote sqlserver server records in the login record

(15) @@ ROWCOUNT: Return a sql statement affects the number of data lines. All SQL statement does not affect the data in the database, the global variable to return the result is 0. During database programming, often @@ ROWCOUNT to detect the return value, in order to clear the operation performed has reached the target.

(16) @@ SPID: Returns the current ID identifies the server process.

(17) @@ TOTAL_ERRORS: sqlserver server returns since launch, the total number of read and write errors encountered.

(18)@@TOTAL_READ:返回自sqlserver服务器启动以来,读磁盘的次数。

(19)@@TOTAL_WRITE:返回自sqlserver服务器启动以来,写磁盘的次数。

(20)@@TRANCOUNT:返回当前连接中,处于活动状态事务的数目。

(21)@@VERSION:返回当前sqlserver服务器安装日期、版本,以及处理器的类型。

Guess you like

Origin www.cnblogs.com/Sungeek/p/10945374.html