首頁 >資料庫 >mysql教程 >SQL Server 2005/2008遍历所有表统计行数

SQL Server 2005/2008遍历所有表统计行数

WBOY
WBOY原創
2016-06-07 14:55:131334瀏覽

在SQL Server 2005/2008中的当前数据库中遍历所有表显示所有表的行数 SQL Server DECLARE CountTableRecords CURSOR READ_ONLY FOR SELECT sst.name, Schema_name(sst.schema_id) FROM sys.tables sst WHERE sst.TYPE = 'U' DECLARE @name VARCHAR(80), @sche

在SQL Server 2005/2008中的当前数据库中遍历所有表显示所有表的行数 SQL Server
DECLARE CountTableRecords CURSOR READ_ONLY FOR 
  SELECT sst.name, 
         Schema_name(sst.schema_id) 
  FROM   sys.tables sst 
  WHERE  sst.TYPE = 'U' 
DECLARE @name   VARCHAR(80), 
        @schema VARCHAR(40) 

OPEN CountTableRecords 

FETCH NEXT FROM CountTableRecords INTO @name, @schema 

WHILE ( @@FETCH_STATUS <> -1 ) 
  BEGIN 
      IF ( @@FETCH_STATUS <> -2 ) 
        BEGIN 
		PRINT @name
                DECLARE @sql NVARCHAR(1024) 
		SET @sql='DECLARE @count INT SELECT @count=COUNT(1) FROM ' + Quotename(@schema) 
                           + 
                           '.' + Quotename(@name) +' PRINT @count'
                  EXEC Sp_executesql @sql 
        END 

      FETCH NEXT FROM CountTableRecords INTO @name, @schema 
  END 

CLOSE CountTableRecords 

DEALLOCATE CountTableRecords 

GO
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn