日期:2014-05-16  浏览次数:20466 次

SQL 合并的问题
循环语句中进行查询,如何把结果合并
------解决方案--------------------
请问你的循环语句是怎么写的?
------解决方案--------------------
引用:
刚才网络有问题~~突然上不去网~
declare @i int
set @i=1
while @i<14
begin 
select * from (
select distinct a.DataTime,b.FileName,b.SheetName,
b.SheetType,b.ReportName,b.CoreReportName,
a.DisplayName
from a,b 
where a.wsObtainID=b.wsObtainID 
) tg
where tg.DataTime=2000+@i 

set @i=@i+1 
end

希望把结果合并成一横行


用临时表把:

if OBJECT_ID('tempdb..#temp') is not null
  drop table #temp

select * into #temp from (
select distinct a.DataTime,b.FileName,b.SheetName,
b.SheetType,b.ReportName,b.CoreReportName,
a.DisplayName
from a,b 
where a.wsObtainID=b.wsObtainID 
) tg
where 1=2


declare @i int
set @i=1
while @i<14
begin 
insert into #temp
select * from (
select distinct a.DataTime,b.FileName,b.SheetName,
b.SheetType,b.ReportName,b.CoreReportName,
a.DisplayName
from a,b 
where a.wsObtainID=b.wsObtainID 
) tg
where tg.DataTime=2000+@i 

set @i=@i+1 
end


select *
from #temp