kosnkov
kosnkov

Reputation: 5941

SQL Server stored procedure: set with into variable

How can I set result from with query into variable?

I have variable

declare @ResultVar float;

and with query

 WITH CTE AS
 (
        SELECT [Val]...
 ) 
 SELECT SUM([Val]) 
 FROM CTE

I don't want to use SELECT, this is part of a SQL Server stored procedure.

Upvotes: 0

Views: 738

Answers (1)

Andomar
Andomar

Reputation: 238086

Per @marc_s's comment:

 WITH CTE AS
 (
        SELECT [Val]...
 ) 
 SELECT @YourVar = SUM([Val]) 
 FROM CTE

A select that assigns a variable does not return a rowset to the caller of the stored procedure. Try it.

Upvotes: 3

Related Questions