sql把4张表数据合并
1个回答
展开全部
declare @max int,@i int = 1
declare @tableName Varchar(16)
select * into #resultTable from 20140101 where 1= 2
select ROW_NUMBER() over (order by table_name) id,TABLE_NAME into #tmp
from information_schema.tables where CONVERT(varchar(6),TABLE_NAME,110) = '201401'
select * from #tmp
select @max = COUNT(1) from #tmp
while(@i<=@max)
begin
select @tableName = Table_Name from #tmp where id = @i
Exec('Insert into #resultTable select * from '+ @tableName + '')
set @i = @i + 1
end
select #resultTable
drop table #tmp
drop table #resultTable
给你个实例去参考:http://bbs.csdn.net/topics/390346193
declare @tableName Varchar(16)
select * into #resultTable from 20140101 where 1= 2
select ROW_NUMBER() over (order by table_name) id,TABLE_NAME into #tmp
from information_schema.tables where CONVERT(varchar(6),TABLE_NAME,110) = '201401'
select * from #tmp
select @max = COUNT(1) from #tmp
while(@i<=@max)
begin
select @tableName = Table_Name from #tmp where id = @i
Exec('Insert into #resultTable select * from '+ @tableName + '')
set @i = @i + 1
end
select #resultTable
drop table #tmp
drop table #resultTable
给你个实例去参考:http://bbs.csdn.net/topics/390346193
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |