sql server 查询所有表的记录条数
DECLARE @tableName NVARCHAR(MAX)
DECLARE @sql NVARCHAR(MAX) = ''
CREATE TABLE #TempTable (TableName SYSNAME, RowCounts BIGINT)
-- 遍历所有用户表
DECLARE TableCursor CURSOR FOR
SELECT t.NAME
FROM sys.tables t
WHERE t.is_ms_shipped = 0 -- 过滤掉系统表
AND t.name NOT LIKE 'sys%' -- 可选:过滤掉以'.sys'开头的系统表
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @tableName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = N'SELECT ''' + @tableName + ''', COUNT(1) AS RowCounts FROM ' + QUOTENAME(@tableName)
INSERT INTO #TempTable EXEC sp_executesql @sql
FETCH NEXT FROM TableCursor INTO @tableName
END
CLOSE TableCursor
DEALLOCATE TableCursor
-- 输出结果
SELECT TableName, RowCounts
FROM #TempTable
ORDER BY RowCounts DESC;
-- 清理临时表
DROP TABLE #TempTable;
原文地址:https://blog.csdn.net/wangqiaowq/article/details/135845632
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!