Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Running totals in return streams?
Message
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
00823275
Message ID:
00823356
Views:
14
>> Can a table returned with and SP be parsed and manipulated in this way?

Almost. While you can't directly manipulate the content of the result set, you can cache the result in a table variable, manipulate them and then return the contents of the table variable.

DELCARE @tablevar TABLE (...., runningTotal int)

INSERT INTO @tablevar (...) SELECT ...

Calculating the running total is easy. It's just the sum of the rows that come before the current row, plus the value in the current row. The hard part is identifying the row that come before the current row.
UPDATE @tablevar
SET runningTotal = value + (SELECT sum(value) FROM @tablevar t WHERE t.seq <= seq)
-Mike

>Is anyone willing to share a technique for returning a calculated value from within a stored procedure. I am using an SP to return a list of account activity but need a running balance. Can a table returned with and SP be parsed and manipulated in this way? Thanks.
Michael Levy
MCSD, MCDBA
ma_levy@hotmail.com
Previous
Reply
Map
View

Click here to load this message in the networking platform