遍历一个数据库中的所有具有XX_aa_bb格式的表
如题,请各位帮忙看下,我要查询一个数据库(TFDB)中所有的表名为XX_aa_bb格式的表,这些表的列名相同,并且查出所有这些表的sum(sl),sum(je)
在此先谢谢各位了
[解决办法]
use TFDB
go
declare @s varchar(max)
select @s=isnull(@s+' union all ','')+'select sum(sl) as sl,sum(je) as je from '+Name from sys.tables where name like '__[_]aa[_]bb'
exec('select sum(sl) as sl,sum(je) as je from ('+@s+') as a')