获得用户表的名字user 数据库名
select name from dbo.sysobjects where type='U'user DB1
select count(*) from dbo.sysobjects where type='U'

解决方案 »

  1.   

    获得所有表的记录数user DB1
    select count(*) from dbo.sysobjects
      

  2.   

    declare @DatabaseName nvarchar(128)
    set @DatabaseName = 'DB1'
    declare @s varchar(8000)
    select @s = case when @s is null then 'use ' + @DatabaseName else @s + ' union' end
        + ' select ''' + [name] + ''' as TableName, (select count(1) from ' + [name] + ') as Num'
    from sysobjects where xtype = 'U'
    exec(@s)
      

  3.   

    declare @DatabaseName nvarchar(128)
    set @DatabaseName = 'DB1'   --数据库名称
    declare @s varchar(8000)
    select @s = case when @s is null then 'use ' + @DatabaseName else @s + ' union' end
        + ' select ''' + [name] + ''' as 表名称, (select count(1) from ' + [name] + ') as 记录数'
    from sysobjects where xtype = 'U'
    exec(@s)
      

  4.   

    use DB1
    if object_id('tempdb..##') is not null drop table ##
    select cast(null as sysname) as 表名称, 1 as 记录数 into ## where 1 = 0
    declare @TableName sysname
    declare testcur cursor for select [name] from sysobjects where xtype ='U' order by [name]
    open testcur
    fetch next from testcur into @TableName
    while @@fetch_status = 0
    begin
      exec('insert into ## select ''' + @TableName + ''', (select count(1) from ' + @TableName + ')')
      fetch next from testcur into @TableName
    end
    close testcur
    deallocate testcur
    select * from ##
    drop table ##