T-SQL,在一次选择中更新多个变量

2021-09-10 00:00:00 sql tsql sql-server

是否可以在一次选择中更新多个局部变量?

Is it possible to update more than one local variable in a single select?

类似于:

set
    @variableOne = avg(someColumn),
    @variableTwo = avg(otherColumn)
    from tblTable

为像这个任务这样微不足道的事情做两个单独的选择操作似乎有点浪费:

It seems a bit wasteful to make two separate select operations for something as trivial as this task:

set @variableOne = ( select avg(someColumn) from tblTable )
set @variableTwo = ( select avg(otherColumn) from tblTable )

推荐答案

是这样的:

select @var1 = avg(someColumn), @var2 = avg(otherColumn) 
from theTable

相关文章