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
try this (cursor free looping):
working example:
OUTPUT:
Well its very easy to loop through the rows in sql procedure u just need to use cursor, i am giving you an example here, for it let us consider a table Employee with column NAME and AGE with 50 records into it and u have to execute a stored procedure say TESTPROC which will take name and age parameters of each row.
Make sure you deallocate the cursor to avoid errors.
You need to create a cursor to loop through the record set.
Example Table:
Cursor:
Here is a link to MSDN on how to create them.
http://msdn.microsoft.com/en-us/library/ms180169.aspx
This is why I used Raise Error instead of PRINT for output.
http://structuredsight.com/2014/11/24/wait-wait-dont-tell-me-on-second-thought/