views:

209

answers:

2

I need to iterate over a recordset from a stored procedure and execute another stored procedure using each fields as arguments. I can't complete this iteration in the code. I have found samples on the internets, but they all seem to deal with a counter. I'm not sure if my problem involved a counter. I need the T-SQL equivalent of a foreach

Currently, my first stored procedure stores its recordset in a temp table, #mytemp. I assume I will call the secondary stored procedure like this:

while (something)
    execute nameofstoredprocedure arg1, arg2, arg3
end
+1  A: 

You need to create a cursor to loop through the record set.

http://www.eggheadcafe.com/PrintSearchContent.asp?LINKID=141

Here is a link to MSDN on how to create them.

http://msdn.microsoft.com/en-us/library/ms180169.aspx

Kevin
I was able to port the eggheadcafe.com link to my code. thanks
David
A: 

try this (cursor free looping):

CREATE TABLE #Results  (RowID  int identity(1,1), Col1  varchar(5), Col2 int, ... )
DECLARE @Current  int
       ,@End      int
DECLARE @Col1  varchar(5)
       ,@Col2 int
       ,...

--you need to capture the result set from the primary stored procedure
INSERT INTO #Results
    (Col1, COl2,...)
    EXEC nameofstoredprocedure_1 arg1, arg2, arg3
SELECT @End=@@ROWCOUNT,@Current=0

--process each row in the result set
WHILE @Current<@End
BEGIN
    SET @Current=@Current+1

    SELECT
        @Col1=COl1, @Col2=Col2
        FROM #Results
        WHERE RowID=@Current

    --call the secondary procedure for each row
    EXEC nameofstoredprocedure_2  @Col1, @Col2,...

END

working example:

CREATE PROCEDURE nameofstoredprocedure_1
(@arg1 int, @arg2 int, @arg3 int)
AS
SELECT 'AAA',@arg1 UNION SELECT 'BBB',@arg2 UNION SELECT 'CCC',@arg3
GO

CREATE PROCEDURE nameofstoredprocedure_2
(@P1 varchar(5), @P2 int)
AS
PRINT '>>'+ISNULL(@P1,'')+','+ISNULL(CONVERT(varchar(10),@P2),'')
GO

CREATE TABLE #Results  (RowID  int identity(1,1), Col1  varchar(5), Col2 int)
DECLARE @Current  int
       ,@End      int
DECLARE @Col1  varchar(5)
       ,@Col2 int


INSERT INTO #Results
    (Col1, COl2)
    EXEC nameofstoredprocedure_1 111, 222, 333
SELECT @End=@@ROWCOUNT,@Current=0

WHILE @Current<@End
BEGIN
    SET @Current=@Current+1

    SELECT
        @Col1=COl1, @Col2=Col2
        FROM #Results
        WHERE RowID=@Current

    EXEC nameofstoredprocedure_2  @Col1, @Col2

END

OUTPUT:

(3 row(s) affected)
>>AAA,111
>>BBB,222
>>CCC,333
KM
when i tried this with my code, the loop never executed with 21 records in #mytemp
David
It works for me. between the `SELECT @End=@@ROWCOUNT,@Current=0` and `WHILE @Current<@End` add this: `print 'will process '+ISNULL(CONVERT(varchar(5),@End),'')+' rows'` and between the `SET @Current=@Current+1` and the `SELECT...` add this: `print ' processing row '+ISNULL(CONVERT(varchar(5),@Current),'')` Also, if you are NOT populating #Results with the insert and running the WHILE at the same time you can add this, before the while: `SELECT @End=COUNT(*) FROM #Results`
KM
cursors are bad, post your modification of my answer and I'll point out your error. I'd rather poke out my eye before I'd use a cursor.
KM