取得数据库(sqlserver)中所有表的记录数

本文提供了一个SQL脚本示例,展示了如何通过声明变量和使用游标来批量处理数据库中的表,实现对每个表的数据计数并存储结果。此过程包括创建临时表存储结果、打印及执行动态SQL。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

declare @sql varchar(200)
declare @tn nvarchar(20)
declare @table table(tn nvarchar(30),sql nvarchar(100)) 
insert into @table select table_name,'insert into ##temp select '''+table_name+''', count(*)  from '+table_name   from INFORMATION_SCHEMA.TABLES where table_type='BASE TABLE'
--delete ##temp
 --select * from @table
create table ##temp(tn nvarchar(30),c int)

   Declare C_CLY CURSOR for
     select tn,sql from @table
   open C_CLY
   fetch next from C_CLY
    into @tn,@sql
   while @@FETCH_STATUS=0
    begin
print @sql
     exec(@sql)
 fetch next from C_CLY
     into @tn,@sql
    end
   close C_CLY
   DEALLOCATE C_CLY
select * from ##temp where c>0

 


--select * from  INFORMATION_SCHEMA.TABLES where table_type='BASE TABLE' 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值