Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Set Value of Select Max or Sum Into Variable

I try to make a stored procedure on SQL Server but when I set the select results into one variable it returns NULL.

example:

set @myvar= select Sum(Values) from myTable

but when I make a select without a function, the variable returns the expected value.

like image 935
Carlos Lopez Avatar asked Oct 15 '25 14:10

Carlos Lopez


1 Answers

You have a mixture of the two ways to set a variable here.

It should be either.

set @myvar = (select sum(Values) from myTable)

OR my preference would be

select @myvar = sum(Values) from myTable
like image 99
Sean Lange Avatar answered Oct 18 '25 07:10

Sean Lange



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!