查找具有指定列名的所有表并将值设置为 NULL
Find all tables with specified column name and set values to NULL
我知道如何在数据库中查找所有具有指定列名的 table:
SELECT TABLE_NAME
, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%Name%'
结果我得到了 table 和 TABLE_NAME
和 COLUMN_NAME
。是否可以将查询接收到的列中的所有值设置为 NULL?
sp_MSforeachtable
未被广泛使用。它遍历每个 table 并执行指定的语句。 [?]
是 table 名称的替身。请在尝试此操作之前备份您的数据库。
EXEC sp_MSforeachtable 'UPDATE [?] SET SomeColumn = NULL'
使用Dynamic Sql
。试试这个。
DECLARE @sql NVARCHAR(max)=''
SELECT @sql += 'update ' + TABLE_NAME + ' set ' + COLUMN_NAME + '= NULL '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%Name%'
--print @sql
EXEC Sp_executesql @sql
您不能定义每个 table 和查询中的每一列。为此,您需要动态地 select tables 和列并执行它。您可以对此动态使用游标
DECLARE @TableName VARCHAR(100)=''
DECLARE @ColumnName VARCHAR(100)
DECLARE @PreviousTableName VARCHAR(100)=''
DECLARE @PreviousColumnName NVARCHAR(MAX)=''
DECLARE @ROWNO INT
DECLARE @MAXCOUNT INT
DECLARE @UPDATESQL NVARCHAR(MAX)
DECLARE @COLSSQL NVARCHAR(MAX)
-- Here you declare which all columns you need to loop in Cursor
DECLARE CUR CURSOR FOR
SELECT TABLE_NAME, COLUMN_NAME
-- Find Row number for each table
,ROW_NUMBER() OVER(PARTITION BY TABLE_NAME ORDER BY COLUMN_NAME) RNO,
-- Gets the count of columns in each table
COUNT(COLUMN_NAME) OVER(PARTITION BY TABLE_NAME) CNT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%TE%'
ORDER BY TABLE_NAME, COLUMN_NAME
OPEN CUR
FETCH NEXT FROM CUR
INTO @TableName,@ColumnName,@ROWNO,@MAXCOUNT
WHILE @@FETCH_STATUS = 0
BEGIN
SET @PreviousColumnName = @PreviousColumnName + @ColumnName + '=NULL,'
-- If current row is last record of the table in the loop
IF(@ROWNO = @MAXCOUNT)
BEGIN
SET @UPDATESQL = 'UPDATE '+@TableName+' SET ' + @PreviousColumnName
SET @UPDATESQL = LEFT(@UPDATESQL, LEN(@UPDATESQL) - 1)
EXEC SP_EXECUTESQL @UPDATESQL
SET @PreviousColumnName = ''
END
-- Fetches next record and increments the loop
FETCH NEXT FROM CUR
INTO @TableName,@ColumnName,@ROWNO,@MAXCOUNT
END
CLOSE CUR;
DEALLOCATE CUR;
我知道如何在数据库中查找所有具有指定列名的 table:
SELECT TABLE_NAME
, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%Name%'
结果我得到了 table 和 TABLE_NAME
和 COLUMN_NAME
。是否可以将查询接收到的列中的所有值设置为 NULL?
sp_MSforeachtable
未被广泛使用。它遍历每个 table 并执行指定的语句。 [?]
是 table 名称的替身。请在尝试此操作之前备份您的数据库。
EXEC sp_MSforeachtable 'UPDATE [?] SET SomeColumn = NULL'
使用Dynamic Sql
。试试这个。
DECLARE @sql NVARCHAR(max)=''
SELECT @sql += 'update ' + TABLE_NAME + ' set ' + COLUMN_NAME + '= NULL '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%Name%'
--print @sql
EXEC Sp_executesql @sql
您不能定义每个 table 和查询中的每一列。为此,您需要动态地 select tables 和列并执行它。您可以对此动态使用游标
DECLARE @TableName VARCHAR(100)=''
DECLARE @ColumnName VARCHAR(100)
DECLARE @PreviousTableName VARCHAR(100)=''
DECLARE @PreviousColumnName NVARCHAR(MAX)=''
DECLARE @ROWNO INT
DECLARE @MAXCOUNT INT
DECLARE @UPDATESQL NVARCHAR(MAX)
DECLARE @COLSSQL NVARCHAR(MAX)
-- Here you declare which all columns you need to loop in Cursor
DECLARE CUR CURSOR FOR
SELECT TABLE_NAME, COLUMN_NAME
-- Find Row number for each table
,ROW_NUMBER() OVER(PARTITION BY TABLE_NAME ORDER BY COLUMN_NAME) RNO,
-- Gets the count of columns in each table
COUNT(COLUMN_NAME) OVER(PARTITION BY TABLE_NAME) CNT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%TE%'
ORDER BY TABLE_NAME, COLUMN_NAME
OPEN CUR
FETCH NEXT FROM CUR
INTO @TableName,@ColumnName,@ROWNO,@MAXCOUNT
WHILE @@FETCH_STATUS = 0
BEGIN
SET @PreviousColumnName = @PreviousColumnName + @ColumnName + '=NULL,'
-- If current row is last record of the table in the loop
IF(@ROWNO = @MAXCOUNT)
BEGIN
SET @UPDATESQL = 'UPDATE '+@TableName+' SET ' + @PreviousColumnName
SET @UPDATESQL = LEFT(@UPDATESQL, LEN(@UPDATESQL) - 1)
EXEC SP_EXECUTESQL @UPDATESQL
SET @PreviousColumnName = ''
END
-- Fetches next record and increments the loop
FETCH NEXT FROM CUR
INTO @TableName,@ColumnName,@ROWNO,@MAXCOUNT
END
CLOSE CUR;
DEALLOCATE CUR;