wdbates
asked on
Updating columns in all tables in a SQL 2008r2 database
Dear Expert;
I need to update 2 columns in all tables in a database where these columns are blank or NULL. These 2 columns are CreatedBy and CreatedOn where CreatedBy = ‘System’ and CreatedOn = GETDATE(). I can obtain the SCHEMA and TABLE name where these columns are present with the following query:
select c.TABLE_SCHEMA, c.TABLE_NAME
from information_schema.columns c
INNER JOIN information_schema.TABLES t ON c.TABLE_NAME = t.TABLE_NAME
WHERE t.TABLE_TYPE = 'BASE TABLE' AND COLUMN_NAME = 'CreatedOn'
How do I loop through this list and update the columns with the default values? I suspect using a CTE with the list of SCHEMAs and TABLEs is a good starting place.
I need to update 2 columns in all tables in a database where these columns are blank or NULL. These 2 columns are CreatedBy and CreatedOn where CreatedBy = ‘System’ and CreatedOn = GETDATE(). I can obtain the SCHEMA and TABLE name where these columns are present with the following query:
select c.TABLE_SCHEMA, c.TABLE_NAME
from information_schema.columns
INNER JOIN information_schema.TABLES t ON c.TABLE_NAME = t.TABLE_NAME
WHERE t.TABLE_TYPE = 'BASE TABLE' AND COLUMN_NAME = 'CreatedOn'
How do I loop through this list and update the columns with the default values? I suspect using a CTE with the list of SCHEMAs and TABLEs is a good starting place.
ASKER
Msg 16916, Level 16, State 1, Line 9
A cursor with the name 'mycur' does not exist.
Msg 16917, Level 16, State 2, Line 16
Cursor is not open.
Msg 16917, Level 16, State 1, Line 33
Cursor is not open.
A cursor with the name 'mycur' does not exist.
Msg 16917, Level 16, State 2, Line 16
Cursor is not open.
Msg 16917, Level 16, State 1, Line 33
Cursor is not open.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Scott, one of the best solutions I've ever seen. Thank you!
Thank you too for the very nice comment!
Open in new window