首页 > 开发 > 综合 > 正文

统计整个库所有表的记录数

2024-07-21 02:49:49
字体:
来源:转载
供稿:网友
统计整个库所有表的记录数
DECLARE @TableName VARCHAR(119)CREATE TABLE #GetRecordingTempTable([id] [INT] IDENTITY(1,1) NOT NULL, [TableName] varchar(255) NOT NULL, [RecordingCount] INT);DECLARE Table_Cursor CURSOR FOR SELECT [name] FROM sysobjects WHERE xtype='U';OPEN Table_Cursor;FETCH NEXT FROM Table_Cursor INTO @TableName;WHILE(@@FETCH_STATUS=0)BEGINEXEC('INSERT INTO #GetRecordingTempTable ([TableName],[RecordingCount]) SELECT '''+@TableName+''', COUNT(0) FROM ['+@TableName+'];');FETCH NEXT FROM Table_Cursor INTO @TableName;ENDCLOSE Table_Cursor;DEALLOCATE Table_Cursor;SELECT [TableName] AS [表名称],[RecordingCount] AS [总记录数] FROM #GetRecordingTempTable ORDER BY [RecordingCount] DESC;DROP TABLE #GetRecordingTempTable;


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表