SQL Server存储过程里 select语句里能加入while循环查询吗?或者说怎么循环查询?
我之前用while循环里加select语句,得出来的结果出现在循环次数一样多的表里。怎么能放到同一张表里?WHILE@StartID<@EndIDBEGINSELECTD...
我之前用while循环里加select语句,得出来的结果出现在循环次数一样多的表里。怎么能放到同一张表里?
WHILE @StartID < @EndID
BEGIN
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 展开
WHILE @StartID < @EndID
BEGIN
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 展开
2个回答
展开全部
第一种方法,创建临时表
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)
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
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
追问
不行啊。。那个table定义之后还是有错误出现!
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |