In SQL we use declare to define local variables, and we can use set and select to assign variables. The order of operations for local variables is to declare, then assign, then use, and the process of declaring and assigning values in different statements
Declaration of variables
For example
declare @name nvarchar (), @id int
Set @name = ' John '
set @int =1
Update set name= @name from student where id= @id
Note
1, the declared variable name must be preceded by the @ number,
2. When using set to assign a value to a variable, you can only assign a value to a variable in one statement, while using a select can assign values to multiple variables, separated by commas, for example:
declare @name nvarchar (a), @id int
Select @name = ' John ', @int =1
update set name= @name from student where id= @id
3, at the same time SELECT statements can be queried in the statement to assign values to the variable
declare @name nvarchar (a)
select @name =name from student where id=1
Second, variable output
When outputting variables, we can use the Select and Print keywords
declare @name nvarchar (a), @id int
Select @name = ' Zhangsan ', @id =10
select @name as [name], @id as [id]
-- The SELECT statement output uses a tabular output of
declare @name nvarchar, @id int
Select @name = ' Zhangsan ', @id =10
print @name
print @id
--print data is output as text, and a statement can output only one variable