AhDung

/*追求代码质量的野生码农*/

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

环境:mssql ent 2k8 r2

原理:遍历所有用户表,用sp_spaceused过程分别获取每张表的行数并写入临时表,最后返回临时表

IF OBJECT_ID('tempdb..#TableRowCount','U') IS NOT NULL DROP TABLE #TableRowCount
GO
CREATE TABLE #TableRowCount (Name sysname PRIMARY KEY, RowCnt DECIMAL(11,0), Reserved VARCHAR(18), Data VARCHAR(18), Index_Size VARCHAR(18), Unused VARCHAR(18))
GO
DECLARE curCntAllTableRows CURSOR LOCAL FAST_FORWARD FOR
        SELECT name FROM sys.tables
OPEN curCntAllTableRows
DECLARE @TableName sysname
FETCH NEXT FROM curCntAllTableRows INTO @TableName
WHILE @@FETCH_STATUS=0
BEGIN
 INSERT INTO #TableRowCount EXEC sys.sp_spaceused @TableName
 FETCH NEXT FROM curCntAllTableRows INTO @TableName
END
CLOSE curCntAllTableRows
DEALLOCATE curCntAllTableRows

SELECT Name,RowCnt FROM #TableRowCount WHERE RowCnt>0
ORDER BY RowCnt DESC,Name

文毕。

posted on 2014-01-28 16:51  ahdung  阅读(2018)  评论(0编辑  收藏  举报