在Sql Server 2005中,如何写存储过程,得到数据库中各个表的记录数? 5

 我来答
百度网友bfb9204
2012-10-26 · TA获得超过718个赞
知道小有建树答主
回答量:1008
采纳率:66%
帮助的人:429万
展开全部
给你一个思路:
1:先查询数据库中有多少表放到一张表中
select indentity(int,1,1)name
into #a
from sysobjects where xtype='U'
2:
统计一共有多少张表,循环执行取得每张表的名字,
来查询表的记录数放到一张表b中
3:
查询存放记录的表

你先自己动手写写看,不会的话我在帮你写
追问
我参照网络资料写了个存储过程,怎么加入条件判断,比如name like '%公司%'???我贴我的存储过程

set nocount on
if object_id(N'tempdb.db.#temp') is not null
drop table #temp
create table #temp (name sysname,count numeric(18))
insert into #temp
select o.name,i.rows
from sysobjects o,sysindexes i
where o.id=i.id and o.Xtype='U' and i.indid<2
select * from #temp
set nocount off
追答
1:首先创建两张表
--create table FFF1 (id int)
--create table FFF_SUM(name2 char(50),id int)
2:创建存储过程
create proc tFFF_SUM
As
select identity(int,1,1)as id ,name as name1
into #a
from sysobjects where xtype='U' --把

查询的表放到临时表中
declare @total int,@i int,@count Int,@name char(50),@aa varchar(200) --定义一些变量
set @i=1
select @total=count(*) from #a --获得总表数
while @i<@total+1 --循环次数
begin
delete from FFF1 --清空上次的比数
select @name=name1 from #a where id=@i --循环得到表的名字
set @aa='select count(*) from ' +QUOTENAME(@name) --动态执行语句

insert into FFF1
exec(@aa) --把表的记录数方法哦表FFF1中
set @i=@i+1
select @count=id from FFF1 --把数量赋值给变量@count
inSERT INTO FFF_SUM
select @name,@count -- 把表名和记录数放到表FFF_SUM 中
end
--select *from FFF_SUM 存储过程执行完 查询每张表的情况
--最好先用用户表少的数据库来测试
摆渡浮桥
2012-10-27 · TA获得超过3629个赞
知道大有可为答主
回答量:1610
采纳率:0%
帮助的人:2188万
展开全部
一种很高效的方法:
select SCHEMA_NAME(t.schema_id) + '.' + t.name as tablename,i.rowcnt
from sysindexes as i , sys.tables as t
where i.id = t.object_id and i.indid in (0,1);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2012-10-26
展开全部
获取表名,循环读取
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式