且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server 2005 - 达到表行大小限制

更新时间:2022-06-19 00:27:55

此处的此查询将确定哪些表具有潜在危险,并且其最大行大小可能会超过可用的 8060 字节:

This query here will determine which tables are potentitally dangerous and have a possible maximum row size that would exceed the 8060 bytes available:

;WITH TableRowSizes AS
(
    SELECT 
        t.NAME 'TableName',
        COUNT(1) 'NumberOfColumns',
        SUM (c.max_length) 'MaxRowLength'
    FROM   
        sys.columns c
    INNER JOIN 
        sys.tables t ON c.object_id = t.object_id
    WHERE
        c.user_type_id NOT IN (98, 165, 167, 231)  -- sql_variant, varbinary, varchar, nvarchar
    GROUP BY 
        t.name
)
SELECT *
FROM TableRowSizes
WHERE MaxRowLength > 8060
ORDER BY MaxRowLength DESC

这并不意味着您的行实际使用超过 8060 个字节 - 它只是总结了每列可能的最大大小.

This doesn't mean your rows are actually using more than 8060 bytes - it just sums up the possible maximum size for each column.

如果您想确定当前实际使用的大小,您可能可以通过检查 DATALENGTH(colname) 函数来做类似的事情(而不是使用 sys.columns 中的理论最大值)

If you want to determine the actual currently used size, you probably can do something similar by examining the DATALENGTH(colname) function (instead of using the theoretical max value from sys.columns)

更新:根据 gbn 的响应在我的 CTE SELECT 中添加了 WHERE 子句 - 这些类型不应用于确定行是否可能突破 8060 字节大小限制.

Update: added a WHERE clause to my CTE SELECT based on gbn's response - those types should not be used in determining if the row potentially breaks the 8060 bytes size limit.