6

我正在制作一个功能。select 语句只返回一行一列,比如一个 int。如何将此 int 存储在声明的变量中,以便我的函数可以返回该变量?

select sum(table_name.col1) -- this line returns only one int. How to store that
--in a declared variable ? 
from
(select 
col1, col2
--code here
)table_name
4

2 回答 2

19
DECLARE @Varname int
SELECT @Varname = SUM(table_name.col1) FROM etc
SELECT @Varname
于 2013-04-11T21:05:10.043 回答
4
DECLARE @result INT

SELECT @result = sum(table_name.col1)
from
(select 
 col1, col2
 --code here
)table_name
于 2013-04-11T21:05:25.830 回答