SQLTeam.com | Weblogs | Forums

Avoiding a WHILE LOOP

#1

Lets say I have something like this:

SELECT 'SELECT * FROM Table1'
UNION
SELECT 'SELECT * FROM Table2'
UNION
SELECT 'SELECT * FROM Table3'
UNION
SELECT 'SELECT * FROM Table4'
UNION
SELECT 'SELECT * FROM Table5'

I would like to EXEC each of the select statement in each row. Usually, I use a LOOP with an EXECUTE statement. Is there a way around this without using a loop?

#2

Use a cursor.
(never thought I would ever say that though)
https://docs.microsoft.com/en-us/sql/relational-databases/cursors?view=sql-server-2017