Definition, assignment, and use of __ databases for local variables

Source: Internet
Author: User
Tags define local

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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.