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.
wdbatesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
That will require a cursor.  Using your SQL statement above, give this a whirl..

Declare @schema_name varchar(10), @table_name varchar(50) , @sql nvarchar(1000) 

Declare @getdate datetime = GETDATE()
Declare @getdate_vc varchar(50) = CONVERT(varchar, @getdate, 121) 

-- Delete all rows from these tables
DECLARE cur Cursor 
FOR 
select c.TABLE_SCHEMA, c.TABLE_NAME
from information_schema.columns c
	JOIN information_schema.TABLES t ON c.TABLE_NAME = t.TABLE_NAME  
WHERE t.TABLE_TYPE = 'BASE TABLE' AND COLUMN_NAME = 'CreatedOn'

FETCH NEXT FROM cur INTO @schema_name, @table_name
WHILE (@@FETCH_STATUS <> -1)  
	BEGIN
	IF (@@FETCH_STATUS <> -2)

		SELECT @sql = 'UPDATE ' + @schema_name + '.' + @table_name + ' '
		SELECT @sql = @sql + 'SET CreatedBy=''system'', CreatedOn=''' + @getdate_vc + ''' '
		SELECT @sql = @sql + 'WHERE ISNULL(CreatedOn, '''') = '''''

		-- At first run with the below line enabled to make sure the T-SQL is correct
		SELECT @sql

		-- Then run with the below line enabled to execute
		-- exec sp_executesql @sql

	FETCH NEXT FROM cur INTO @schema_name, @table_name	
	END
CLOSE cur
DEALLOCATE cur

Open in new window

0
wdbatesAuthor Commented:
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.
0
Scott PletcherSenior DBACommented:
DECLARE cursor_tables CURSOR LOCAL FAST_FORWARD FOR
SELECT
    OBJECT_NAME(c.object_id) AS table_name,
    CAST(MAX(CASE WHEN c.name = 'CreatedOn' THEN 1 ELSE 0 END) AS bit) AS CreatedOn_Found,
    CAST(MAX(CASE WHEN c.name = 'CreatedBy' THEN 1 ELSE 0 END) AS bit) AS CreatedBy_Found
FROM sys.columns c
INNER JOIN sys.types t ON
    t.system_type_id = c.system_type_id AND
    t.user_type_id = c.user_type_id
WHERE
    (c.name = 'CreatedOn' AND t.name LIKE '%date%') OR
    (c.name = 'CreatedBy' AND t.name LIKE '%char%')
GROUP BY c.object_id
DECLARE @table_name varchar(128)
DECLARE @CreatedOn_Found bit
DECLARE @CreatedBy_Found bit

DECLARE @sql_set_CreatedOn varchar(100)
DECLARE @sql_where_CreatedOn varchar(100)
DECLARE @sql_set_CreatedBy varchar(100)
DECLARE @sql_where_CreatedBy varchar(100)
DECLARE @sql varchar(8000)

SET @sql_set_CreatedOn = 'CreatedOn = CASE WHEN CreatedOn IS NULL THEN GETDATE() ELSE CreatedOn END'
SET @sql_where_CreatedOn = 'CreatedOn IS NULL'
SET @sql_set_CreatedBy = 'CreatedBy = CASE WHEN CreatedBy IS NULL OR CreatedBy = '''' THEN ''System'' ELSE '''' END'
SET @sql_where_CreatedBy = 'CreatedBy IS NULL OR CreatedBy = '''''
   
OPEN cursor_tables

WHILE 1 = 1
BEGIN
    FETCH NEXT FROM cursor_tables INTO @table_name, @CreatedOn_Found, @CreatedBy_Found;
    IF @@FETCH_STATUS = -1
        BREAK;
    IF @@FETCH_STATUS = -2
        CONTINUE;
    SET @sql = 'UPDATE [' + @table_name + '] SET ' +
        CASE WHEN @CreatedOn_Found = 1 THEN @sql_set_CreatedOn ELSE '' END +
        CASE WHEN @CreatedBy_Found = 1 THEN CASE WHEN @CreatedOn_Found = 1 THEN ', ' ELSE '' END + @sql_set_CreatedBy
             ELSE '' END
    SET @sql = @sql + ' WHERE ' +
        CASE WHEN @CreatedOn_Found = 1 THEN @sql_where_CreatedOn ELSE '' END +
        CASE WHEN @CreatedBy_Found = 1 THEN CASE WHEN @CreatedOn_Found = 1 THEN ' OR ' ELSE '' END + @sql_where_CreatedBy
             ELSE '' END
    PRINT @sql
END --WHILE

DEALLOCATE cursor_tables
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wdbatesAuthor Commented:
Scott, one of the best solutions I've ever seen.  Thank you!
0
Scott PletcherSenior DBACommented:
Thank you too for the very nice comment!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.