日期:2014-05-18 浏览次数:20555 次
create procedure proc_test AS BEGIN set nocount on declare @sql varchar(8000); declare @id int; declare @dbname varchar(30); declare @tb table(id identity(1,1) int,dbname varchar(100)) insert into @tb(dbname) select 'db1' union all select 'db2' union all select 'db3' union all select 'db4' --... 连你的各种数据库 set @sql="" select top 1 @id,=id,@dbname=dbname from @tb order by id desc while isnull(@id,0)<>0 begin set @sql=@sql+'select * from '+@tb+'..TTT20110101 union all select * from '+@tb+'..TTT20110102 ' if @id<>1 set @sql=@sql+' union all ' delete from @tb where id=@id select top 1 @id,=id,@dbname=dbname from @tb order by id desc end exec (@sql) END --调用方式: exec proc_test --试下,我没测- -
------解决方案--------------------
Select * From 机器名.ABC201101.dbo.TTT20110101 Union Select * From 机器名.ABC201102.dbo.TTT20110101 ……