SQL Server Update 全部表的某一列(列名相同,類型相同)數值

DECLARE @sql VARCHAR(5000);
WITH T
AS (SELECT SchemaName = c.TABLE_SCHEMA,
           TableName = c.TABLE_NAME,
           ColumnName = c.COLUMN_NAME,
           DataType = DATA_TYPE,
           sql = 'update ' + c.TABLE_NAME + ' set ' + c.COLUMN_NAME + ' = ''2017-11-20''' -----------執行語句
    FROM INFORMATION_SCHEMA.COLUMNS c
        INNER JOIN INFORMATION_SCHEMA.TABLES t
            ON c.TABLE_NAME = t.TABLE_NAME
               AND c.TABLE_SCHEMA = t.TABLE_SCHEMA
               AND t.TABLE_TYPE = 'BASE TABLE'
               AND c.COLUMN_NAME LIKE '%CreateTime%' ---列名
               AND c.DATA_TYPE = 'datetime'),
     T2
AS (SELECT STUFF(
           (
               SELECT '; ' + A.sql FROM T A FOR XML PATH('')
           ),
           1,
           1,
           ''
                ) AS sqls)
SELECT @sql = sqls
FROM T2;
PRINT @sql;
EXEC (@sql);
相關文章
相關標籤/搜索