user3448011
user3448011

Reputation: 1599

get a column value from a table and assign it ot a scalar variable on SQL server 2008

I need to get a column value from a table on SQL server 2008.

DECLARE @result TABLE 
(
  val FLOAT
);
insert into @result (val)
(
    select SUM(c)/10 val from atable
)

DECLARE @myval float 
SELECT @myval  = @result.val  # error : Must declare the scalar variable "@result". !!!

      if @myval = null
      begin 
  select @myval
      end  

Why ?

thx !

Upvotes: 0

Views: 364

Answers (1)

suff trek
suff trek

Reputation: 39777

The correct format is

SELECT @myval  = val FROM @result

But I think creating a table is an overkill here. You can do simple

select @myval = SUM(c)/10 from atable

If I understend you correctly.

Or if you simple return result of the sum, just do

select SUM(c)/10 from atable

to get the result back to the caller without any additional perturbations.

Upvotes: 1

Related Questions