2

首先对不起我的英语,我知道它并不完美

我的数据库中有大约 40 列的表,我需要获取该表中某些记录的大小才能生成报告。

我尝试用 dbcc SHOWCONTIG 做一些事情,但没有得到满意的结果。

你有什么建议/想法来解决我的问题吗?感谢帮助!

4

4 回答 4

1

建议使用此处的参考信息编写查询以根据内容进行计算:http: //www.connectionstrings.com/sql-server-data-types-reference/

例如,假设您的表有 2 列: Avarchar(20)和 a datetime。然后您的查询将如下所示:

SELECT RowID, -- Some identifier for the row, e.g. primary key
       LEN(varcharcolumn) + 2 -- Length of varchar is L + 2 bytes
       + 8 -- Length of datetime column is 8 bytes
       ) AS TotalBytes
-- Add WHERE clause to restrict rows in report
于 2013-07-30T14:50:36.723 回答
0

在 sql server 中,很容易获得表的大小。所以另一种方法是将您想要过滤的记录存储在一个单独的表中,然后只获取表的大小。

下面是一个例子:

--CREATE A TABLE TO STORE YOUR FILTERED RECORDS
CREATE TABLE dbo.resulttable
(
    id INT,
    employee VARCHAR(32)
)
GO

--INSERT FILTERED RECORDS IN NEW TABLE
INSERT INTO dbo.resulttable (id,employee)
SELECT id,employee FROM dbo.originaltable WHERE id > 100

GO
--GET SIZE OF TABLE WHICH HAS THE FILTERED RECORDS WITH HOWEVER MANY COLUMNS
SELECT 
    t.name AS TableName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.object_id = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
    t.NAME = 'resulttable' 
GROUP BY 
    t.Name, p.Rows
ORDER BY 
    t.Name

GO
于 2013-08-02T12:51:34.333 回答
0

我不确定我是否完全了解您的需求,但可能DataLength功能会帮助您:

select DataLength(ColumnOfInterest)
from TableName
where <filter condition>

对于一组列:

select DataLength(ColumnOfInterest1) + DataLength(ColumnOfInterest2) + etc.
from TableName
where <filter condition>
于 2013-07-30T13:15:04.797 回答
0

假设您想要数据类型长度,请尝试从 INFORMATION_SCHEMA 中选择以获取表的结构。

就像是

SELECT * FROM INFORMATION_SCHEMA.COLUMNS;

应该足够了。CHARACTER_MAXIMUM_LENGTH 列为您提供字符串长度。

于 2013-07-30T13:12:10.770 回答