我不知道为什么这个查询只返回第一行无限
DECLARE db_cursor1 CURSOR FOR select ilduedt,accountno,ilno from TBPAYSCHED where accountno ='000520285344' and ilno!=0 order by id
DECLARE @ilduedt datetime;
DECLARE @accountno varchar(MAX);
DECLARE @ilno int;
DECLARE @tempdate datetime;
OPEN db_cursor1;
FETCH NEXT FROM db_cursor1 INTO @ilduedt,@accountno,@ilno
WHILE @@FETCH_STATUS = 0
BEGIN
select @ilduedt,@accountno,@ilno
END;
CLOSE db_cursor1;
DEALLOCATE db_cursor1;
tbpaysched
accountno | ilno | ilduedt
------------------------------------------
000520285344 0 2017-07-30 00:00:00.000
000520285344 1 2017-09-15 00:00:00.000
000520285344 2 2017-08-30 00:00:00.000
000520285344 3 2017-09-15 00:00:00.000
它只返回ilno 1无限的行。我不知道,但我想我错过了一些愚蠢的东西。
答案 0 :(得分:1)
你错过了while循环中的花药FETCH NEXT
,应该是:
WHILE @@FETCH_STATUS = 0
BEGIN
select @ilduedt,@accountno,@ilno
FETCH NEXT FROM db_cursor1 INTO @ilduedt,@accountno,@ilno
END
答案 1 :(得分:1)
您没有通过调用FETCH_NEXT从记录集中检索下一行。但是,假设记录集中有一行,上面的代码应该会导致无限循环。
FETCH NEXT FROM db_cursor1 INTO @ilduedt,@accountno,@ilno
WHILE @@FETCH_STATUS = 0
BEGIN
select @ilduedt,@accountno,@ilno
FETCH NEXT FROM db_cursor1 INTO @ilduedt,@accountno,@ilno
END;