I have a table where each record has a Table_Name (name of a table). I then use a Cursor to select all table names related to some record in to a Cursor. Then I do a WHILE for each table name in the Cursor to do some job.
I want to know if it's possible to solve this problem without using a Cursor.
DECLARE tables_cursor CURSOR FAST_FORWARD FOR SELECT Table_Name FROM Some_Table WHERE ...
FETCH NEXT FROM tables_cursor INTO @Dynamic_Table_Name
WHILE @@FETCH_STATUS = 0
BEGIN
...
END
Foreach table name in the cursor I do a dynamic SQL query like this:
SELECT @sql = '
UPDATE dbo.' + @Dynamic_Table_Name + '
SET ...'
EXEC sp_executesql @sql, @params, ...
My question is this: Is it possible to avoid using Cursor to solve this problem?
Unfortunately the design of having table name to reference a table can't be changed, of which I would have done immediately if I could.