Does sql server store selection result in variable?

I am making a function. The select statement returns only one row with one column, for example int. How to save this int inside a declared variable so that my function can return a variable?

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
+5
source share
2 answers
DECLARE @Varname int
SELECT @Varname = SUM(table_name.col1) FROM etc
SELECT @Varname
+18
source
DECLARE @result INT

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

All Articles