Is there a way to do foreach value do something in sql server 2008.
I have a table variable that holds some IDs and i have to some more tasks based on the values.
The table vairable is as follows:
declare @InspectionIDTable table(InspectionID uniqueidentifier not null)
based on the values in this table i need to loop and run some select statements. based on the result i have to populate other table variables and do some more processing.
basically, i required foreach in sql server that can have nested foreach. Is there a clean apporach to do this.
Also, i would like to know whether it is a good approach to do this in Stored Procedure from programming point of view.
You could write a stored procedure and open a cursor on your “resultset”
http://sqlserverplanet.com/tsql/sql-server-for-each-row-next/
Anyway you should avoid to use cursors because their bad performance
There is a way to do row-by-row operations without cursors
Have a look at the below:
http://www.sql-server-performance.com/2004/operations-no-cursors/