SQL查询数据库中每张表的记录数

declare   @tbName     nvarchar(500)
declare   @ct      int   
declare   @csql   nvarchar(500)   
declare   #tb   cursor   for  SELECT OBJECT_NAME (id) As TableName FROM sysobjects WHERE xtype = 'U' AND OBJECTPROPERTY (id, 'IsMSShipped') = 0
open   #tb 
 fetch   next   from   #tb   into   @tbName
 while   @@fetch_status=0 
 begin  
  set @csql = N'Select @ct= Count(*)  From ' + @tbName
  Exec dbo.sp_executesql  @csql,N'@ct   int   output',@ct   output 
  Print @tbName + '---' + Cast(@ct  As  nvarchar(500))
 fetch   next   from   #tb   into   @tbName
 end     
close   #tb 
deallocate   #tb
 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yellow1003/archive/2008/02/01/2077085.aspx

原文地址:https://www.cnblogs.com/longshen/p/1527521.html