第一种方法,创建临时表
create table #temptable()
WHILE @StartID < @EndID
BEGIN
insert into #temptable SELECT。。。
END
第二种方法,使用拼装一个SQL
declare @sql nvarchar(2000)
WHILE @StartID < @EndID
BEGIN
组装一个合适的SQL,使用union all拼起来
set @sql=@sql+''
END
exec(@sql)
WHILE @StartID < @EndID
BEGIN
insert into table
SELECT DX,DY,DZ,DATE
FROM GPSSTATICRESULT
WHERE ID= 1 AND GPSRESULT_DATE>=@StartDate AND GPSRESULT_DATE<=@EndDate AND
GPSRESULT_DATE IN(SELECT TOP 1 GPSRESULT_DATE FROM GPSSTATICRESULT WHERE DATEPART(wk,GPSRESULT_DATE) = DATEPART(wk, @WEEKDATE) and DATEPART(yy,GPSRESULT_DATE) = DATEPART(yy,@WEEKDATE) )
SET @WEEKDATE =DATEADD(DAY,7,@WEEKDATE)
Set @StartID = @StartID +1
end
select * from table