当前位置:网站首页>Sql: stored procedures and triggers - Notes

Sql: stored procedures and triggers - Notes

2022-07-06 21:34:00 Nianchi ichthyology programming

1. Conceptual classification of variables
Concept : A language element assigned a certain value .
classification : Global variables 、 local variable

Global variables Variables provided by the system and declared in advance , Generally, users can only view and cannot modify the value of global variables , In general, it is based on @@ The starting variable
local variable An object declared by a user to hold a single data value of a specific type . It is local to a statement batch , In general, it is based on @ The starting variable

2. Declaration and assignment of variables
Local variables must be declared before use , Basic statement of declaration :

declare @  local variable  [as]  data type  [,......n]

Be careful : Use declare The initial value of the local variable declared by the statement is initialized to NULL.
Variable assignment statement format :

set @  Local variable name  =  value  |  expression 

Be careful : Any expression can be SQL expression
You can also use select Statement to assign a value to a local variable , The format is :

select @  local variable  =  value  |  expression 

Example : Declare three integer variables :@x、@y and @z, And give @x、@y Each variable is assigned an initial value , Then assign the sum value of these two variables to @z, And display variables @z Result .

declare @x int , @y int , @z int
set @x = 10
set @y = 20
set @z = @x + @y
print @z

print The function of is to return user-defined information to the client , Its grammatical form is :

print  'ascll Text string ' | @  Local variable name  |  String expressions  | @@  Function name 
原网站

版权声明
本文为[Nianchi ichthyology programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061307031321.html