SQL SERVER in the SELECT and SET assignment similarities and differences

SET and SELECT in SQL SERVER can be used in variable assignment, but its usage and effects somewhat different in some detail.

1. In terms of variable assignment, SET is the ANSI standard way of assignment, SELECT is not. This is also one of the reasons SET mode is recommended for use.

2. SELECT can assign multiple variables at once, and once only for a SET variable assignment.

DECLARE @NAME NVARCHAR(128), @AGE INT;
SET @NAME = N'小明';
SET @AGE=18;
PRINT @NAME;
PRINT @AGE;
GO
DECLARE @NAME NVARCHAR(128), @AGE INT; SELECT @NAME = N'小明',@AGE=18; PRINT @NAME; PRINT @AGE;

3. When using a subquery to the variable assignment is required subquery query must be scalar (i.e., the result obtained is a subquery or data line a), a plurality of values ​​can not be returned, otherwise an error.

1) But note that if the time to assign values ​​to variables in the SELECT query, the query returns the number of records will not generate an error, the value of the variable is the corresponding value obtained query record last line of.

2) If the result of the query does not return records, which means that the return value is NULL, the entire sub-queries manner assignment, SET and SELECT will be set to NULL, and the assignment in the SELECT query, the variables will remain as the initial value Affected.

IF (OBJECT_ID('tempdb..#temp') is not null)
BEGIN
    DROP TABLE #temp;
END
ELSE
BEGIN
    CREATE TABLE #temp(
    [Name] NVARCHAR(128) ,
    AGE INT
    )
END
GO
INSERT INTO #temp([Name],AGE) VALUES(N'小明',18)
INSERT INTO #temp([Name],AGE) VALUES(N'小张',19)
INSERT INTO #temp([Name],AGE) VALUES(N'小王',17)
GO
DECLARE @NAME1 NVARCHAR(128), @AGE1 INT,@NAME2 NVARCHAR(128), @AGE2 The INT ;
 the SET  @ NAME1 = ( the SELECT  the TOP  . 1  [ NAME ]  the FROM # TEMP );     - the SET scalar query evaluation 
the SELECT  @ AGE1 = ( the SELECT  the TOP  . 1 of AGE the FROM # TEMP );         - the SELECT scalar query evaluation 
the SELECT  @ NAME2 = [ NAME ] , @ Age2 = [ AGE ]  the FROM # the TEMP ;     - the SELECT query statement assignment 
PRINT NAME1 @ ; - run correctly display the results: Xiao Ming 
the PRINT  @ AGE1 ;   - run correctly display the results: 18 
the PRINT  @ NAME2 ; - run correctly, the results show: Wang 
the PRINT  @ Age2 ;   - run correctly display the results :. 17 

the GO 

the DECLARE  @ NAME1  NVARCHAR ( 128 ), @ AGE1  the INT , @ NAME2  NVARCHAR ( 128 ), @ Age2  the INT ;
 the SELECT  @ NAME1 = N ' original name ' , @ AGE1 = 0 , @ NAME2= N ' original name ' , @ Age2 = 0 ; - initialization of the variable values 
the SET  @ NAME1 = ( the SELECT  the TOP  . 1  [ NAME ]  the FROM # TEMP  the WHERE  . 1 > . 1 );     - the SET scalar query evaluation 
the SELECT  @ AGE1 = ( the SELECT  TOP  1 AGE the FROM # the TEMP  the WHERE  1 > 1 );         - the SELECT query scalar assignment in 
the SELECT  @ NAME2= [ NAME ] , @ Age2 = [ AGE ]  the FROM # the TEMP  the WHERE  1 > 1 ;     - the assignment SELECT query in 
the PRINT  @ NAME1 ; - run correctly, the actual value: NULL, display the results :( blank) 
the PRINT  @ AGE1 ;   - correct operation, the actual value: NULL, display the results :( blank) 
the PRINT  @ NAME2 ; - run correctly, and show the actual values: the initial name of 
the PRINT  @ Age2 ;   - run correctly, and show the actual value: 0 
GO

 

 So how do we choose which way:

1. As a result of SET ANSI standard, so it is recommended usage.

2. Under no circumstances considered standard, if it comes to multiple variable assignment, to write less code, or get the value of multiple global variables, consider using a SELECT, first, because simple, and second, some global variables in changes in the second sentence of execution time value.

 

 

Guess you like

Origin www.cnblogs.com/markkang/p/11965732.html