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
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.
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.
DECLARE @Varname int
SELECT @Varname = SUM(table_name.col1) FROM etc
SELECT @Varname
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With