Oliver Oliver - 2 months ago 7
SQL Question

Setting two scalar variables in one SELECT statement?

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


But this is invalid syntax. How do I set multiple scalar variables in one select statement? I can do:

Declare @a int;
Declare @b int;

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

PRINT @a
PRINT @b


But this will take twice as long. What is the fastest way?

Answer
Declare @a int;
Declare @b int;

SELECT @a = StartNum, @b = EndNum 
FROM Users 
Where UserId = '1223'