1

Here is the link to the solution I found and used The code provided in this question should perfectly solve my case.

however, there is only 1 row affected. I wonder if that means I should add a loop or other conditions to make it run through all the tables.

    Declare @ColName as nVarchar(100), @NewValue as nVarchar(50) 
    Set @ColName = 'test_column'  -- 'your col name'
    Set @NewValue = getDate() -- your date time value
    Select 'Update ' + TABLE_NAME + ' set ' + COLUMN_NAME + ' = ''' + @NewValue + '''' From INFORMATION_SCHEMA.COLUMNS Where column_name = 'test_column'
Community
  • 1
  • 1
Dilabeing
  • 31
  • 6

1 Answers1

1

You can update only 1 table at a time. You can update multiple rows in the same table or multiple columns, but not multiple tables in a single update. You would need a loop to iterate for each table of the update.

Select 'UPDATE ' + TABLE_NAME + ' SET CreatedDateTime = ''<<New Value>>'' ' AS SqlStatement INTO #T
From INFORMATION_SCHEMA.COLUMNS 
WHERE COLUMN_NAME = 'CreatedDateTime'

WHILE EXISTS (SELECT * FROM #T) BEGIN
    DECLARE @Sql VARCHAR(4000) = (SELECT TOP 1 SqlStatement FROM #T)
    EXEC (@Sql)
    DELETE #T WHERE SqlStatement = @Sql
END
Jason W
  • 13,026
  • 3
  • 31
  • 62
  • Please feel free to upvote an answer if it helped you and accept it if it solved your question. This helps others to know which answers may have helped with your question. It also increases your reputation. – Jason W Jul 14 '15 at 02:43