I have a nested set of SELECT statements that (eventualy) returns two aggregate functions (SUM) - SUM(CpuLoad) & SUM(MemLoadKybytes)
There are a variety of parameters passed to the select statement, so I have built up the final command from a number of varchars.
I'd like to know how to get the two answers in to two output parameters (@sCPULoad & @sMemLoadKbytes).
THe full stored proceudre is below, but in general terms the statements is as follows;
SET @queryText3 = 'SELECT @sCPULoad=sCL, @sMemLoadKbytes=sML FROM (SELECT SUM(CpuLoad) AS sCL, SUM(MemLoadKbytes) AS sML FROM (SELECT ASProcessID, max(StatsTime)...<remainin
g SELECT statement>)
And I'm calling it as
EXEC @queryText3 @sCPULoad OUT, @sMemLoadKbytes OUT
However, SQL query analyser (& .NET) throws an exception starting with
Server: Msg 203, Level 16, State 2, Procedure usp_PM_PMProcessLoadsByPro
cesses_SUM
, Line 24
[Microsoft][ODBC SQL Server Driver][SQL Server]The name 'SELECT @sCPULoad=sCL, @sMemLoadKbytes=sML FR
But I can't see the full error message because the actual string is too long for the debugger and gets clipped.
Full Procedure listing
ALTER PROCEDURE dbo.usp_PM_PMProcessLoadsB
yProcesses
_SUM
(
@RemoteTable varchar(250),
@ProcessIDs varchar(250),
@TimePoint varchar(50),
@sCPULoad int OUTPUT,
@sMemLoadKbytes int OUTPUT
)
AS
DECLARE @queryText1 varchar(8000)
DECLARE @queryText2 varchar(8000)
DECLARE @queryText3 varchar(8000)
SET @queryText1 = 'SELECT ASProcessID, max(StatsTime) as mST FROM '
+ @RemoteTable +
' WHERE ASProcessID IN (SELECT ID FROM fnSplitStringToIDs(''' + @ProcessIDs + ''')) AND StatsTime <= ''' + @TimePoint + ''' GROUP BY ASProcessID'
SET @queryText2 = 'SELECT SUM(CpuLoad) AS sCL, SUM(MemLoadKbytes) AS sML FROM (' + @queryText1 + ')tTable INNER JOIN '
+ @RemoteTable + ' ON tTable.mST = ' + @RemoteTable + '.StatsTime AND tTable.ASProcessID = ' + @RemoteTable + '.ASProcessID'
SET @queryText3 = 'SELECT @sCPULoad=sCL, @sMemLoadKbytes=sML FROM (' + @queryText2 + ')'
EXEC @queryText3 @sCPULoad OUT, @sMemLoadKbytes OUT
RETURN
Start Free Trial