T-SQL :: 加入 sys.masked_columns returns table 中的所有列
T-SQL :: joining sys.masked_columns returns all columns in table
我正在尝试通过 sys.objects
加入 INFORMATION_SCHEMA.COLUMNS
和 sys.masked_columns
。
这是我的查询:
SELECT
TABLE_SCHEMA,
TABLE_NAME,
COLUMN_NAME,
DATA_TYPE
+ CASE WHEN DATA_TYPE IN ('char', 'nchar', 'varchar', 'nvarchar', 'binary', 'varbinary')
AND CHARACTER_MAXIMUM_LENGTH > 0
THEN COALESCE('(' + CONVERT(varchar, CHARACTER_MAXIMUM_LENGTH) + ')', '')
ELSE ''
END
+ CASE WHEN DATA_TYPE IN ('decimal', 'numeric')
THEN COALESCE('(' + CONVERT(varchar, NUMERIC_PRECISION) + ',' + CONVERT(varchar, NUMERIC_SCALE) + ')', '')
ELSE ''
END AS Declaration_Type,
--CASE WHEN IS_NULLABLE='NO' THEN 'NOT ' ELSE '' END + 'NULL' AS Nullable
m.is_masked,
m.masking_function
FROM
INFORMATION_SCHEMA.COLUMNS c
JOIN
sys.objects o ON c.table_name = o.name
JOIN
sys.masked_columns m ON o.[object_id] = m.[object_id]
ORDER BY
1, 2, 3
和return是这样的:
TABLE_SCHEMA
TABLE_NAME
COLUMN_NAME
Declaration_Type
is_masked
masking_function
Person
EmailAddress
BusinessEntityID
int
1
email()
Person
EmailAddress
EmailAddress
nvarchar(50)
1
email()
Person
EmailAddress
EmailAddressID
int
1
email()
Person
EmailAddress
ModifiedDate
datetime
1
email()
Person
EmailAddress
rowguid
uniqueidentifier
1
email()
但是结果是错误的,因为它显示 Person.EmailAddress
中的所有列都被屏蔽了。
如果我检查这个查询:
SELECT c.name, tbl.name as table_name, c.is_masked, c.masking_function
FROM sys.masked_columns AS c
JOIN sys.tables AS tbl
ON c.[object_id] = tbl.[object_id]
WHERE is_masked = 1;
仅限 SSMS return 1 个屏蔽列:
name
table_name
is_masked
masking_function
EmailAddress
EmailAddress
1
email()
为什么 return 正在 Person.EmailAddress
中的每一列?
感谢@MartinSmith 和@JeroenMostert 在评论中提供的帮助。
正确的解决方案是去掉 INFORMATION_SCHEMA.*
列并改用 sys.
列:
mc.name AS column_name,
mc.is_masked,
mc.masking_function
, [Type] =
CASE
WHEN tp.[name] IN ('varchar', 'char') THEN tp.[name] + '(' + IIF(mc.max_length = -1, 'max', CAST(mc.max_length AS VARCHAR(25))) + ')'
WHEN tp.[name] IN ('nvarchar','nchar') THEN tp.[name] + '(' + IIF(mc.max_length = -1, 'max', CAST(mc.max_length / 2 AS VARCHAR(25)))+ ')'
WHEN tp.[name] IN ('decimal', 'numeric') THEN tp.[name] + '(' + CAST(mc.[precision] AS VARCHAR(25)) + ', ' + CAST(mc.[scale] AS VARCHAR(25)) + ')'
WHEN tp.[name] IN ('datetime2') THEN tp.[name] + '(' + CAST(mc.[scale] AS VARCHAR(25)) + ')'
ELSE tp.[name]
END
FROM sys.masked_columns AS mc
JOIN sys.tables AS tbl ON mc.[object_id] = tbl.[object_id]
JOIN sys.types tp ON mc.user_type_id = tp.user_type_id
WHERE mc.is_masked = 1;
我正在尝试通过 sys.objects
加入 INFORMATION_SCHEMA.COLUMNS
和 sys.masked_columns
。
这是我的查询:
SELECT
TABLE_SCHEMA,
TABLE_NAME,
COLUMN_NAME,
DATA_TYPE
+ CASE WHEN DATA_TYPE IN ('char', 'nchar', 'varchar', 'nvarchar', 'binary', 'varbinary')
AND CHARACTER_MAXIMUM_LENGTH > 0
THEN COALESCE('(' + CONVERT(varchar, CHARACTER_MAXIMUM_LENGTH) + ')', '')
ELSE ''
END
+ CASE WHEN DATA_TYPE IN ('decimal', 'numeric')
THEN COALESCE('(' + CONVERT(varchar, NUMERIC_PRECISION) + ',' + CONVERT(varchar, NUMERIC_SCALE) + ')', '')
ELSE ''
END AS Declaration_Type,
--CASE WHEN IS_NULLABLE='NO' THEN 'NOT ' ELSE '' END + 'NULL' AS Nullable
m.is_masked,
m.masking_function
FROM
INFORMATION_SCHEMA.COLUMNS c
JOIN
sys.objects o ON c.table_name = o.name
JOIN
sys.masked_columns m ON o.[object_id] = m.[object_id]
ORDER BY
1, 2, 3
和return是这样的:
TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | Declaration_Type | is_masked | masking_function |
---|---|---|---|---|---|
Person | EmailAddress | BusinessEntityID | int | 1 | email() |
Person | EmailAddress | EmailAddress | nvarchar(50) | 1 | email() |
Person | EmailAddress | EmailAddressID | int | 1 | email() |
Person | EmailAddress | ModifiedDate | datetime | 1 | email() |
Person | EmailAddress | rowguid | uniqueidentifier | 1 | email() |
但是结果是错误的,因为它显示 Person.EmailAddress
中的所有列都被屏蔽了。
如果我检查这个查询:
SELECT c.name, tbl.name as table_name, c.is_masked, c.masking_function
FROM sys.masked_columns AS c
JOIN sys.tables AS tbl
ON c.[object_id] = tbl.[object_id]
WHERE is_masked = 1;
仅限 SSMS return 1 个屏蔽列:
name | table_name | is_masked | masking_function | |
---|---|---|---|---|
EmailAddress | EmailAddress | 1 | email() |
为什么 return 正在 Person.EmailAddress
中的每一列?
感谢@MartinSmith 和@JeroenMostert 在评论中提供的帮助。
正确的解决方案是去掉 INFORMATION_SCHEMA.*
列并改用 sys.
列:
mc.name AS column_name,
mc.is_masked,
mc.masking_function
, [Type] =
CASE
WHEN tp.[name] IN ('varchar', 'char') THEN tp.[name] + '(' + IIF(mc.max_length = -1, 'max', CAST(mc.max_length AS VARCHAR(25))) + ')'
WHEN tp.[name] IN ('nvarchar','nchar') THEN tp.[name] + '(' + IIF(mc.max_length = -1, 'max', CAST(mc.max_length / 2 AS VARCHAR(25)))+ ')'
WHEN tp.[name] IN ('decimal', 'numeric') THEN tp.[name] + '(' + CAST(mc.[precision] AS VARCHAR(25)) + ', ' + CAST(mc.[scale] AS VARCHAR(25)) + ')'
WHEN tp.[name] IN ('datetime2') THEN tp.[name] + '(' + CAST(mc.[scale] AS VARCHAR(25)) + ')'
ELSE tp.[name]
END
FROM sys.masked_columns AS mc
JOIN sys.tables AS tbl ON mc.[object_id] = tbl.[object_id]
JOIN sys.types tp ON mc.user_type_id = tp.user_type_id
WHERE mc.is_masked = 1;