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

被折叠的 条评论
为什么被折叠?



