SQL Server获取数据库中超过450个Unicode字符或900个字节的所有列

时间:2018-07-17 06:37:21

标签: sql sql-server tsql

我想获取数据库中所有超过450个Unicode字符或900个字节的列(SQL Server)

1 个答案:

答案 0 :(得分:1)

使用此查询查看将引发以下警告的索引:

  

警告!最大密钥长度为900字节。索引“ IndexName”   最大长度为1900字节。对于一些大的组合   值,插入/更新操作将失败。

SELECT 
    SchemaName = ss.name, 
    TableName = so.name, 
    IndexName = si.name, 
    KeyColumnMaxLength = SUM(sc.max_length)
FROM 
    sys.objects so 
    JOIN sys.columns sc ON so.object_id = sc.object_id
    JOIN sys.schemas ss ON so.schema_id = ss.schema_id
    JOIN sys.indexes si ON sc.object_id = si.object_id
    JOIN sys.index_columns sic ON 
        si.object_id = sic.object_id AND 
        si.index_id = sic.index_id AND 
        sic.column_id = sc.column_id
WHERE
    so.type = 'U' AND 
    sic.is_included_column = 0
GROUP BY 
    ss.name, 
    so.name, 
    si.name
HAVING 
    SUM(sc.max_length) > 900
ORDER BY 
    SUM(sc.max_length) DESC