I have a collection (100+) of tables all of which contain two fields
RowChanged bit
ChangedFields bit
Now, an error has occurred which leave some entries with RowChanged = 1 while ChangedFields is empty. I therefore need to go through these and set RowChanged = 0 where ChangedFields empty.
I have achieved this by the following cursor.
BEGIN TRANSACTION
USE DatabaseName --Database name to clean
DECLARE @Table_Name VARCHAR(50)
DECLARE @Query VARCHAR(250)
DECLARE Table_Cursor CURSOR FOR SELECT Name FROM sys.tables;
DECLARE @Affected_Rows INTEGER = 0
OPEN Table_Cursor
FETCH NEXT FROM Table_Cursor INTO @Table_Name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Query = 'Update '+@Table_Name+' Set RowChanged = 0 Where RowChanged = 1 And (LEN(RTRIM(CONVERT(NVARCHAR(100), ChangedFields))) = 0 OR ChangedFields IS NULL)'
EXEC (@Query)
SET @Affected_Rows = @Affected_Rows + COALESCE(@@ROWCOUNT, 0)
FETCH NEXT FROM Table_Cursor INTO @Table_Name
END
SELECT @Affected_Rows AS Affected_Rows
CLOSE Table_Cursor
DEALLOCATE Table_Cursor
ROLLBACK --Change to COMMIT in order to save changes
While this does work, I have a genetic aversion against using cursors. Also I have just learned that Apply can in many cases achieve what Cursors did pre-2005.
What I need to do is to go though all tables in the database and check for the condition where RowChanged = 1 and ChangedFields like '' or NULL.
I have tried working this out with TVFs and what not, but I keep coming up short. While I could do operations in a single table, getting the list from sys.tables and doing something on several tables have eluded me.