海海日记-冯海滨博客

您现在的位置是:首页 > SQL > 正文

SQL

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

happyfhb2013-05-26SQL1528
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


平淡中储蓄成长

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~