select count(*) from table

解决方案 »

  1.   

    是一个库中,不是某一个表结果:tabname     rowstab1         1111
    tab2         12456
    tab3         45612
    tab4         45128
    ...
      

  2.   

    exec sp_msforeachtable 'select ''?'',count(*) from ?'
      

  3.   

    to: lynx1111(任我行:一个PLMM看着就兴奋的男人)sp_msforeachtable 这个系统过程具体语法有吗?贴来学学,谢谢!
      

  4.   

    在分析器中运行:
    或写成过程!
    create table ##tableCount(
    tableName varchar(500),
    colCount int
    )
    --insert into ##tableCount (tableName,colCount) select * from 
    --exec sp_msforeachtable 'select ''?'' tableName, count(*) colCount into ##tableCount from ?'exec sp_msforeachtable 'insert into ##tableCount (tableName,colCount) select ''?'' tableName, count(*) colCount from ?'select * from ##tableCountdrop table ##tableCount
      

  5.   

    declare @id        int                        
    declare @type        character(2)                 
    declare        @pages        
    int                        
    declare @dbname sysname
    declare @dbsize dec(15,0)
    declare @bytesperpage        dec(15,0)
    declare @pagesperMB                dec(15,0)create table #spt_space
    (
            objid                int null,
            rows                int null,
            reserved        dec(15) null,
            data                dec(15) null,
            indexp                dec(15) null,
            unused                dec(15) null
    )set nocount on-- Create a cursor to loop through the user   tables
    declare c_tables cursor for
    select        id
    from        sysobjects
    where        xtype = 'U'open c_tablesfetch next from c_tables
    into @idwhile @@fetch_status = 0
    begin        /* Code from sp_spaceused */
            insert into #spt_space (objid, reserved)
                    select objid = @id, sum(reserved)
                            from sysindexes
                                    where indid in (0, 1, 255)
                                            and id = @id        select @pages = sum(dpages)
                            from sysindexes
                                    where indid < 2
                                            and id = @id
            select @pages = @pages + isnull(sum(used), 0)
                    from sysindexes
                            where indid = 255
                                    and id = @id
            update #spt_space
                    set data = @pages
            where objid = @id
            /* index: sum(used) where indid in (0, 1, 255) - data */
            update #spt_space
                    set indexp = (select sum(used)
                                    from sysindexes
                                    where indid in (0, 1, 255)
                                    and id = @id)
                                - data
                    where objid = @id        /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
            update #spt_space
                    set unused = reserved
                                    - (select sum(used)
                                            from sysindexes
                                                    where indid in (0, 1, 255)
                                                    and id = @id)
                    where objid = @id        update #spt_space
                    set rows = i.rows
                            from sysindexes i
                                    where i.indid < 2
                                    and i.id = @id
                                    and objid = @id        fetch next from c_tables
            into @id
    endselect         TableName = (select left(name,60) from sysobjects where id = objid),
            Rows = convert(char(11), rows),
            ReservedKB = ltrim(str(reserved * d.low / 1024.,15,0) + ' ' + 'KB'),
            DataKB = ltrim(str(data * d.low / 1024.,15,0) + ' ' + 'KB'),
            IndexSizeKB = ltrim(str(indexp * d.low / 1024.,15,0) + ' ' + 'KB'),
            UnusedKB = ltrim(str(unused * d.low / 1024.,15,0) + ' ' + 'KB')
                    
    from         #spt_space, master.dbo.spt_values d
    where         d.number = 1
    and         d.type = 'E'
    order by reserved desc 
    drop table #spt_space
    close c_tables
    deallocate c_tables你先测试一下看看效果,这个是从 sqlservercentral.com上找到来,属于我的珍藏系列呢: )
      

  6.   

    declare @id        int                        
    declare @type        character(2)                 
    declare        @pages        
    int                        
    declare @dbname sysname
    declare @dbsize dec(15,0)
    declare @bytesperpage        dec(15,0)
    declare @pagesperMB                dec(15,0)create table #spt_space
    (
            objid                int null,
            rows                int null,
            reserved        dec(15) null,
            data                dec(15) null,
            indexp                dec(15) null,
            unused                dec(15) null
    )set nocount on-- Create a cursor to loop through the user   tables
    declare c_tables cursor for
    select        id
    from        sysobjects
    where        xtype = 'U'open c_tablesfetch next from c_tables
    into @idwhile @@fetch_status = 0
    begin        /* Code from sp_spaceused */
            insert into #spt_space (objid, reserved)
                    select objid = @id, sum(reserved)
                            from sysindexes
                                    where indid in (0, 1, 255)
                                            and id = @id        select @pages = sum(dpages)
                            from sysindexes
                                    where indid < 2
                                            and id = @id
            select @pages = @pages + isnull(sum(used), 0)
                    from sysindexes
                            where indid = 255
                                    and id = @id
            update #spt_space
                    set data = @pages
            where objid = @id
            /* index: sum(used) where indid in (0, 1, 255) - data */
            update #spt_space
                    set indexp = (select sum(used)
                                    from sysindexes
                                    where indid in (0, 1, 255)
                                    and id = @id)
                                - data
                    where objid = @id        /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
            update #spt_space
                    set unused = reserved
                                    - (select sum(used)
                                            from sysindexes
                                                    where indid in (0, 1, 255)
                                                    and id = @id)
                    where objid = @id        update #spt_space
                    set rows = i.rows
                            from sysindexes i
                                    where i.indid < 2
                                    and i.id = @id
                                    and objid = @id        fetch next from c_tables
            into @id
    endselect         TableName = (select left(name,60) from sysobjects where id = objid),
            Rows = convert(char(11), rows),
            ReservedKB = ltrim(str(reserved * d.low / 1024.,15,0) + ' ' + 'KB'),
            DataKB = ltrim(str(data * d.low / 1024.,15,0) + ' ' + 'KB'),
            IndexSizeKB = ltrim(str(indexp * d.low / 1024.,15,0) + ' ' + 'KB'),
            UnusedKB = ltrim(str(unused * d.low / 1024.,15,0) + ' ' + 'KB')
                    
    from         #spt_space, master.dbo.spt_values d
    where         d.number = 1
    and         d.type = 'E'
    order by reserved desc 
    drop table #spt_space
    close c_tables
    deallocate c_tables
      

  7.   

    用的着这么麻烦吗?直接取sysobject where type='U',循环打印就可以了。
      

  8.   

    create table ##tableCount(
    tableName varchar(500),
    colCount int)
    exec sp_msforeachtable 'insert into ##tableCount (tableName,colCount) select ''?'' tableName, count(*) colCount from ?'
    select * from ##tableCount order by colCount
    drop table ##tableCount--实现楼主的功能;
    --自己也学习了sp_msforeachtable的用法
    --谢谢: lynx1111(任我行:一个PLMM看着就兴奋的男人)
      

  9.   

    --这个方法要求数据库内库表不能太多,太多时就要考虑使用游标循环处理了declare @sql varchar(8000)
    set @sql = 'select '
    select @sql = @sql + '
    [table] = ''' + name + ''',
    [rows]=(select count(*) from ' + name +') 
     union all select '
    from sysobjects a where a.xtype='U'
    print left(@sql,len(@sql)-20)
      

  10.   

    转:从SQLSERVER6.5开始,MS提供了一个非常有用的系统存储过程sp_MSforeachtable和sp_MSforeachDB;作为DBA会经常需要检查所有的数据库或用户表,比如:检查所有数据库的容量;看看指定数据库所有用户表的容量,所有表的记录数...,我们一般处理这样的问题都是用游标分别处理处理,比如:在数据库检索效率非常慢时,我们想检查数据库所有的用户表,我们就必须这样写游标:
    DECLARE @TableName varchar(255)
    DECLARE @ExeSQL varchar(4000)DECLARE Table_Cursor CURSOR FOR SELECT [name] FROM sysobjects WHERE xtype='U'OPEN Table_Cursor
    FETCH NEXT FROM  Table_Cursor INTO @TableNameWHILE(@@FETCH_STATUS=0)
    BEGIN
     PRINT @TableName
     SELECT @ExeSQL='DBCC CHECKTABLE('''+@TableName+''')'
     EXEC(@EXESQL)
    FETCH NEXT FROM  Table_Cursor INTO @TableName
    ENDCLOSE Table_Cursor
    DEALLOCATE Table_Cursor
    GO    如果我们用sp_MSforeachtable就可以非常方便的达到相同的目的:
    EXEC sp_MSforeachtable @command1="print '?' DBCC CHECKTABLE('?')"
    大家可以看出这样就更加简洁(虽然在后台也是通过游标来处理的),下面我们就仔细分析一下sp_MSforeachtable这个存储过程:我们看看sp_MSforeachtable详细的CODE:
    USE MASTER 
    GO
    SP_HELPTEXT sp_MSforeachtable--下面时sp_MSforeachtable的原始代码CREATE proc sp_MSforeachtable
     @command1 nvarchar(2000), @replacechar nchar(1) = N'?', @command2 nvarchar(2000) = null,
       @command3 nvarchar(2000) = null, @whereand nvarchar(2000) = null,
     @precommand nvarchar(2000) = null, @postcommand nvarchar(2000) = null
    as
     /* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its own result set */
     /* @precommand and @postcommand may be used to force a single result set via a temp table. */ /* Preprocessor won't replace within quotes so have to use str(). */
     declare @mscat nvarchar(12)
     select @mscat = ltrim(str(convert(int, 0x0002))) if (@precommand is not null)
      exec(@precommand) /* Create the select */
       exec(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' + REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '
             + N' where OBJECTPROPERTY(o.id, N''IsUserTable'') = 1 ' + N' and o.category & ' + @mscat + N' = 0 '
             + @whereand)
     declare @retval int
     select @retval = @@error
     if (@retval = 0)
      exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3 if (@retval = 0 and @postcommand is not null)
      exec(@postcommand) return @retval这个系统存储过程有7个参数:
     @command1 nvarchar(2000),  --第一条运行的T-SQL指令
     @replacechar nchar(1) = N'?',   --指定的占位符号 
     @command2 nvarchar(2000) = null,--第二条运行的T-SQL指令
        @command3 nvarchar(2000) = null, --第三条运行的T-SQL指令
     @whereand nvarchar(2000) = null, --可选条件来选择表
     @precommand nvarchar(2000) = null, --在表前执行的指令
     @postcommand nvarchar(2000) = null --在表后执行的指令
    所以上面的语句也可以这样写:
    EXEC sp_MSforeachtable @command1="print '?'",
             @command2= "DBCC CHECKTABLE('?')"了解参数以后,就让我们做几个实列吧:
    1.获得每个表的记录数和容量:
    EXEC sp_MSforeachtable @command1="print '?'",
             @command2="sp_spaceused '?'",
             @command3= "SELECT count(*) FROM ? "2.更新PUBS数据库中已t开头的所有表的统计:
    EXEC sp_MSforeachtable @whereand="and name like 't%'",
             @replacechar='*',
             @precommand="print 'Updating Statistics.....' print ''",
             @command1="print '*' update statistics * ",
             @postcommand= "print''print 'Complete Update Statistics!'"
    sp_MSforeachDB除了@whereand外,和sp_MSforeachtable的参数是一样的,我们可以通过这个存储过程检测所有的数据库,比如:
    1.获得所有的数据库的存储空间:
           EXEC sp_MSforeachdb  @command1="print '?'",
                                             @command2="sp_spaceused "
    2.检查所有的数据库
           EXEC sp_MSforeachdb  @command1="print '?'",
                                               @command2="DBCC CHECKDB (?) "有了上面的分析,我们可以建立自己的sp_MSforeachObject:
    USE MASTER
    GO
    CREATE proc sp_MSforeachObject
     @objectType int=1,
     @command1 nvarchar(2000), 
     @replacechar nchar(1) = N'?', 
     @command2 nvarchar(2000) = null,
        @command3 nvarchar(2000) = null, 
     @whereand nvarchar(2000) = null,
     @precommand nvarchar(2000) = null, 
     @postcommand nvarchar(2000) = null
    as
     /* This proc returns one or more rows for each table (optionally, matching @where), with each table defaulting to its own result set */
     /* @precommand and @postcommand may be used to force a single result set via a temp table. */ /* Preprocessor won't replace within quotes so have to use str(). */
     declare @mscat nvarchar(12)
     select @mscat = ltrim(str(convert(int, 0x0002))) if (@precommand is not null)
      exec(@precommand) /* Defined  @isobject for save object type */
     Declare @isobject varchar(256) select @isobject= case @objectType when 1 then 'IsUserTable'
             when 2 then 'IsView'
             when 3 then 'IsTrigger'
             when 4 then 'IsProcedure' 
             when 5 then 'IsDefault'   
             when 6 then 'IsForeignKey'
             when 7 then 'IsScalarFunction'
             when 8 then 'IsInlineFunction'
             when 9 then 'IsPrimaryKey'
             when 10 then 'IsExtendedProc'    
             when 11 then 'IsReplProc'
             when 12 then 'IsRule'
                      end /* Create the select */
     /* Use @isobject variable isstead of IsUserTable string */
    EXEC(N'declare hCForEach cursor global for select ''['' + REPLACE(user_name(uid), N'']'', N'']]'') + '']'' + ''.'' + ''['' + REPLACE(object_name(id), N'']'', N'']]'') + '']'' from dbo.sysobjects o '
            + N' where OBJECTPROPERTY(o.id, N'''+@isobject+''') = 1 '+N' and o.category & ' + @mscat + N' = 0 '
           + @whereand) declare @retval int
     select @retval = @@error
     if (@retval = 0)
      exec @retval = sp_MSforeach_worker @command1, @replacechar, @command2, @command3 if (@retval = 0 and @postcommand is not null)
      exec(@postcommand) return @retvalGO
    这样我们来测试一下:
    1.获得所有的存储过程的脚本:
             EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=4
    2.获得所有的视图的脚本:
             EXEc sp_MSforeachObject @command1="sp_helptext '?' ",@objectType=2
    3.比如在开发过程中,没一个用户都是自己的OBJECT OWNER,所以在真实的数据库时都要改为DBO:
               EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=1
               EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=2
                EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=3
                  EXEc sp_MSforeachObject @command1="sp_changeobjectowner '?', 'dbo'",@objectType=4
      这样就非常方便的将每一个数据库对象改为DBO.
      

  11.   

    ’可以利用SQL语句然后生成一个SQL语句就行了。
    SELECT 'UNION SELECT '''+ NAME+ ''' AS TABNAME,COUNT(*)'+' FROM '+NAME+' AS FLDCOUNT' FROM SYSOBJECTS WHERE XTYPE='U'
    将生成的第一个UNION去掉,然后在SQL里面执行就OK了。
      

  12.   

    --统计数据库中表中行数
    create table ##tableCount(
    tableName varchar(500),
    colCount int)
    exec sp_msforeachtable 'insert into ##tableCount (tableName,colCount) select ''?'' tableName, count(*) colCount from ?'
    select * from ##tableCount order by colCount
    drop table ##tableCount
      

  13.   

    谢谢各位,现在知道还有一个 sp_msforeachtable 的