sql server在变量中存储select的结果?

时间:2022-04-26 15:41:49

I am making a function. A select statement returns only one row with one column, say an int. How do i store this int inside a declared variable so that my function can return the variable ?

我正在做一个功能。 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

2 个解决方案

#1


18  

DECLARE @Varname int
SELECT @Varname = SUM(table_name.col1) FROM etc
SELECT @Varname

#2


4  

DECLARE @result INT

SELECT @result = sum(table_name.col1)
from
(select 
 col1, col2
 --code here
)table_name

#1


18  

DECLARE @Varname int
SELECT @Varname = SUM(table_name.col1) FROM etc
SELECT @Varname

#2


4  

DECLARE @result INT

SELECT @result = sum(table_name.col1)
from
(select 
 col1, col2
 --code here
)table_name