Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql server store the result of a select in a variable?

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 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
like image 475
david blaine Avatar asked Apr 11 '13 21:04

david blaine


People also ask

How do I use a variable in SQL Select statement?

Variables in SQL procedures are defined by using the DECLARE statement. Values can be assigned to variables using the SET statement or the SELECT INTO statement or as a default value when the variable is declared. Literals, expressions, the result of a query, and special register values can be assigned to variables.

How do I store a list of values in one column in SQL Server?

In the design all users data will be stored in a series of columns in a single table but one of the columns requires to store a list of values, for example: 'column1' will store the username , 'column2' will store the userID and 'column3' will store a list of items that will change over time.


1 Answers

DECLARE @Varname int
SELECT @Varname = SUM(table_name.col1) FROM etc
SELECT @Varname
like image 190
Facio Ratio Avatar answered Oct 12 '22 23:10

Facio Ratio