What is the difference between the set and select assignment modes in SQL ?, Sqlselect

Source: Internet
Author: User

What is the difference between the set and select assignment modes in SQL ?, Sqlselect

The variables in the T-SQL are divided into local variables and global variables, the use of local variables is first declared, and then assigned a value. Global variables are defined and maintained by the system, which can be used directly, but are generally not defined.

Local variable

In the T-SQL, the name of a local variable must be prefixed with a tag.

Declaer @ variable_name DataType

Where @ variable_name is the variable name DataType is the data type.

You can assign values to local variables by using the Set or Select statements.

Set @ variable_name = value

Or

Selecte @ variable_name = value

Ø the SET value assignment statement is generally used to assign data variables specified by variables.

The Select value assignment statement is generally used to query data in a table. If more than one record is queried, the value of the last record is assigned to the variable.

 

Set

Select

Assign values to multiple variables at the same time

Not Supported

Supported

The expression returns multiple values.

Error

Assign the last returned value to the variable.

The expression does not return a value.

The variable is assigned a NULL value.

Keep original variable value

We recommend that you use Set because the Set statement has no results and consumes less resources. Therefore, it is more efficient to use Set when assigning values to a variable.



Note: SELECT can also assign the value of the scalar quantum query to the variable. If the scalar query does not return a value, the variable is SET to null (in this case, the value is exactly the same as that of the SET query ).
The concept of benchmarking quantum queries may be unfamiliar to everyone. The following is an example (compared with the green section above ):

Declare @ name varchar (128)
Set @ name = 'select initiator'
Select @ name = (select name from fuzhiTEST where id = 4)
Print @ name -- run correctly: displays NULL
GO



Related Article

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.