在一个SELECT语句中设置两个标量变量?

问题描述

DECLARE @a int;
DECLARE @b int;

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

解决方法

我想做这个:

Declare @a int;
Declare @b int;

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

PRINT @a
PRINT @b

但这是无效的语法。如何在一个select语句中设置多个标量变量?我可以:

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

但这将花费两倍的时间。最快的方法是什么?