T-SQL有助于获取每个表的行数,列数和大小

时间:2016-08-09 14:43:27

标签: sql-server

我正在尝试获取表名行数列数表的大小。我已经编写了这个存储过程,但是我没有得到正确的行数:

    int[] firstPosition = new int[2];
    int[] secondPosition = new int[2];

    firstView.getLocationOnScreen(firstPosition);
    secondView.getLocationOnScreen(secondPosition);

    // Rect constructor parameters: left, top, right, bottom
    Rect rectFirstView = new Rect(firstPosition[0], firstPosition[1],
            firstPosition[0] + firstView.getMeasuredWidth(), firstPosition[1] + firstView.getMeasuredHeight());
    Rect rectSecondView = new Rect(secondPosition[0], secondPosition[1],
            secondPosition[0] + secondView.getMeasuredWidth(), secondPosition[1] + secondView.getMeasuredHeight());
    return rectFirstView.intersect(rectSecondView);

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

您可以加入sysindexes表格来计算行数:

ALTER PROCEDURE abc (@table_name SYSNAME)
AS
BEGIN

SELECT 
    t.name, 
    count(c.column_id) AS no_of_cols, 
    MAX(i.rows) AS no_of_rows
FROM 
    sys.tables t

    INNER JOIN sys.columns c 
    ON t.object_id = c.object_id

    INNER JOIN sysindexes i 
    ON i.id = t.object_id    
WHERE 
    t.name = @table_name    
GROUP BY 
    t.name;

END

N.B。此方法查看表统计信息,可能不准确。您可以使用DBCC UPDATEUSAGE(db) WITH COUNT_ROWS重建统计信息。

答案 1 :(得分:1)

这应该对你有帮助:

ALTER PROCEDURE abc (@TableName SYSNAME)
AS
BEGIN

DECLARE @SizeTable Table
(
name Varchar(100),
rows bigint,
reserved Varchar(30),
data Varchar(30),
index_size Varchar(30),
unused Varchar(30)
)

INSERT INTO @SizeTable EXEC sp_spaceused @TableName

;With cteColumns as 
(
    select COUNT(*) as ColumnsCount from sys.columns 
    WHERE object_id = OBJECT_ID(@TableName)
)

SELECT * FROM @SizeTable as st
CROSS JOIN cteColumns
END