Setting two scalar variables in one SELECT statement?

前端 未结 3 1746
走了就别回头了
走了就别回头了 2021-02-01 01:30

I want to do this:

Declare @a int;
Declare @b int;

SET @a,@b = (SELECT StartNum,EndNum FROM Users Where UserId = \'1223\')

PRINT @a
PRINT @b

相关标签:
3条回答
  • 2021-02-01 01:49

    Do it like this:

    Declare @a int;
    Declare @b int;
    
    SELECT @a=StartNum,@b=EndNum FROM Users Where UserId = '1223'
    
    PRINT @a
    PRINT @b
    
    0 讨论(0)
  • 2021-02-01 01:50

    If you are doing this in a stored procedure and don't want the result of the select in an output resultset you will need to use the word INTO.

    Declare @a int;
    Declare @b int;
    
    SELECT StartNum, EndNum 
    FROM Users 
    Where UserId = '1223'
    INTO @a, @b;
    

    It also can be used like this:

    SELECT StartNum, EndNum 
    INTO @a, @b
    FROM Users 
    Where UserId = '1223';
    
    0 讨论(0)
  • 2021-02-01 02:08
    DECLARE @a int;
    DECLARE @b int;
    
    SELECT @a = StartNum, @b = EndNum 
    FROM Users 
    WHERE UserId = '1223'
    
    0 讨论(0)
提交回复
热议问题