0

我正在研究在 SQL Datawarehouse 的几个数据库中屏蔽个人数据。我创建了一个表,其中包含我想要屏蔽的列,并希望通过这样的单个脚本更改数据库中多个表中存在的这些列

IF EXISTS(SELECT *
          FROM INFORMATION_SCHEMA.COLUMNS
          WHERE TABLE_NAME = 'TableName' and 
          COLUMN_NAME = 'BirthDate' OR COLUMN_NAME = 'EmailAddress' OR COLUMN_NAME = 'FirstName' OR COLUMN_NAME = 'LastName' OR COLUMN_NAME = 'MiddleName' OR COLUMN_NAME = 'Name.First' OR COLUMN_NAME = 'MName.Last' OR COLUMN_NAME = 'PhoneNumber'
          )
BEGIN
ALTER TABLE TABLE_NAME
ALTER COLUMN [COLUMN_NAME] NVARCHAR(200) MASKED WITH (FUNCTION = 'default()') NULL
END

这不起作用。有任何想法吗?

4

2 回答 2

0

你需要使用循环来使用动态 sql 执行你的 DDL 代码

DECLARE @tmp TABLE (ID int identity, TableName varchar(100), ColumnName varchar(100)

INSERT @tmp
SELECT QUOTENAME(TABLE_NAME), QUOTENAME(COUMN_NAME)
          FROM INFORMATION_SCHEMA.COLUMNS
          WHERE TABLE_NAME = 'TableName' and 
          COLUMN_NAME = 'BirthDate' OR COLUMN_NAME = 'EmailAddress' OR COLUMN_NAME = 'FirstName' OR COLUMN_NAME = 'LastName' OR COLUMN_NAME = 'MiddleName' OR COLUMN_NAME = 'Name.First' OR COLUMN_NAME = 'MName.Last' OR COLUMN_NAME = 'PhoneNumber'
          )
DECLARE @ID int, @TableName varchar(100), @ColumnName varchar(100)
DECLARE @SQL varchar(MAX)
WHILE EXISTS (SELECT 1 FROM @tmp)
BEGIN
    SELECT TOP 1 @ID = ID, @TableName = TableName, @ColumnName = ColumnName

    SET @SQL = '
    ALTER TABLE ' + @TableName + '
    ALTER COLUMN ' + @ColumnName + ' NVARCHAR(200) MASKED WITH (FUNCTION = ''default()'') NULL
    '
    EXEC(@SQL)
    DELETE @tmp WHERE ID = @ID
END
于 2019-02-18T15:01:43.753 回答
0

您必须使用动态 SQL 来执行此操作。使用您对 Information_Schema 的查询来动态生成所需的 ALTER TABLE 语句,然后执行它们。

于 2019-02-18T14:29:50.080 回答