获取SQL Server中插入行的大小

时间:2016-08-30 09:03:42

标签: sql sql-server database sql-server-2012 sql-server-2012-express

我想知道是否有办法找出SQL Server中特定行使用的总字节数。我可以通过计算所有nvarchar列的总和来获得最大行大小,但是当我们有一个nvarchar(MAX)或二进制列时,它会产生一个问题。我正在寻找确切的没有。硬盘上一行使用的字节数。

这样的事情可能吗?

3 个答案:

答案 0 :(得分:4)

尝试以下:

declare @table nvarchar(128)
declare @idcol nvarchar(128)
declare @sql nvarchar(max)

--initialize those two values
set @table = 'YourTable'
set @idcol = 'some id to recognize the row'

set @sql = 'select ' + @idcol +' , (0'

select @sql = @sql + ' + isnull(datalength(' + name + '), 1)' 
    from sys.columns where object_id = object_id(@table)
set @sql = @sql + ') as rowsize from ' + @table + ' order by rowsize desc'

PRINT @sql

exec (@sql)

行将按大小排序,因此您可以从上到下进行检查。

答案 1 :(得分:1)

您可以在sql中对varbinary使用DATALENGTH Funtion。

虽然它是特定于列的,但您可以使用where子句将其用于所有列以获取行

<div id="heading">2015 Annual Architectural Design Convention</div>

#heading {
    width: 100%;
}

答案 2 :(得分:-1)

使用这个,将对一行的所有列进行数据长度

select
    SomeOtherColumn,
    Bytes = datalength((select x.* from (values(null))data(bar) for xml auto))
from Table