declare @sql varchar(8000)
declare @tb_name sysnamedeclare cur cursor for
  select name from sysobjects where xtype='U'open curfetch next from cur into @tb_namewhile @@fetch_status=0
begin
    exec ('select tb_name='''+@tb_name+''',count(*) cnt from ['+@tb_name+']')    fetch next from cur into @tb_name
endclose cur
deallocate cur

解决方案 »

  1.   

    --以sql server 2000自带PUBS库为例:SELECT name FROM sysobjects WHERE xtype= 'U'  
    union all
    select '总数 = ' + cast(sum(i.rows) as varchar) from sysindexes i,sysobjects o
    where i.id = o.id
    and o.xtype='U'
    and i.indid<2/*
    name                 
    ---------------------
    titleauthor
    stores
    sales
    roysched
    discounts
    jobs
    pub_info
    Table1
    Table2
    authors
    A
    B
    publishers
    titles
    总数 = 232(所影响的行数为 15 行)
    */
      

  2.   

    SELECT name FROM sysobjects WHERE xtype= 'U'  
    union all
    select o.name+'总数 = ' + cast(sum(i.rows) as varchar) from sysindexes i,sysobjects o
    where i.id = o.id
    and o.xtype='U'
    and i.indid<2
    group by o.name
    order by name/*
    name  
    -------
    authors
    authors总数 = 23
    discounts
    discounts总数 = 3
    dtproperties
    dtproperties总数 = 0
    employee
    employee总数 = 43
    jobs
    jobs总数 = 14
    pub_info
    pub_info总数 = 8
    publishers
    publishers总数 = 8
    roysched
    roysched总数 = 86
    sales
    sales总数 = 21
    stores
    stores总数 = 6
    tb
    tb总数 = 3
    titleauthor
    titleauthor总数 = 25
    titles
    titles总数 = 18(所影响的行数为 26 行)
    */
      

  3.   

    declare @tb varchar(50)
    declare @t table (Id int identity,Tbname varchar(50),num int)declare roy cursor for SELECT name FROM sysobjects WHERE xtype= 'U'open royfetch next from roy into @tb
    while @@fetch_status=0
    begin
    declare @sql nvarchar(100),@i intset @sql='select @i=count(1) from '+@tbexec sp_executesql @sql,N'@I int output',@i outputinsert into @t select @tb,@Ifetch next from roy into @tb
    end
    close roy
    deallocate royselect * from @t
      

  4.   


    SELECT name FROM sysobjects WHERE xtype= 'U'  
    union all
    select o.name+'总数 = ' + cast(sum(i.rows) as varchar) from sysindexes i,sysobjects o
    where i.id = o.id
    and o.xtype='U'
    and i.indid<2
    group by o.name
    order by name