Chinaunix首页 | 论坛 | 博客
  • 博客访问: 134109
  • 博文数量: 22
  • 博客积分: 1415
  • 博客等级: 上尉
  • 技术积分: 230
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-23 10:19
文章分类

全部博文(22)

文章存档

2010年(1)

2008年(6)

2007年(12)

2006年(3)

我的朋友

分类: 数据库开发技术

2007-09-11 11:01:33

create table #(id int identity ,tblname varchar(50),num int)
declare @name varchar(30)
declare roy cursor for select name from sysobjects
where xtype='U'
open roy
fetch next from roy into @name
while @@fetch_status=0
begin
declare @i int
declare @sql nvarchar(1000)
set @sql='select @n=count(1) from
exec sp_executesql @sql,N'@n int output',@i output
insert into # select @name,@I
fetch next from roy into @name
end
close roy
deallocate roy
select * from #
 
统计数据库里有多少表以及所有表的记录数
 
declare @sql varchar(8000),@count int,@step int
set nocount on
越大运行速度越快,但如果太大会造成生成的sql字符串超出限制导致语句不完整出错
set @step = 60 
if object_id(N'tempdb.db.#temp') is not null
  drop table #temp
create table #temp (name sysname,count numeric(18))
if object_id(N'tempdb.db.#temp1') is not null
  drop table #temp1
create table #temp1 (id int identity(1,1),name sysname)
insert into #temp1(name) select name from sysobjects where xtype = 'u';
set @count = @@rowcount
while @count>0
begin
  set @sql = ''
  select @sql = @sql + ' select ''' + name + ''',count(1) from ' + name + ' union'
  from #temp1
  where id > @count - @step and id <= @count
  print @sql
  set @sql = left(@sql,len(@sql) - 6)
  print @sql
  insert into #temp exec (@sql)
  set @count = @count - @step
end
select count(count) 总表数,sum(count) 总记录数 from #temp
select * from #temp
set nocount off
阅读(1646) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~