I have a table where each record has a Table_Name (table name). Then I use the cursor to select all the table names associated with some record in the Cursor. Then I do a WHILE for each table name in Cursor to do some work.
I want to know if this problem can be solved 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
The name of the Foreach table in the cursor. I am executing a dynamic SQL query as follows:
SELECT @sql = ' UPDATE dbo.' + @Dynamic_Table_Name + ' SET ...' EXEC sp_executesql @sql, @params, ...
My question is this: is it possible to avoid using the cursor to solve this problem?
Unfortunately, the design with the table name to refer to the table cannot be changed, and I would do it immediately if I could.
source share