我有一个MySQL存储过程,其中包含以下WHILE语句。
我已经确认@RowCnt是1,而@MaxRows是6090,但是经过进一步调试后,我意识到WHILE语句经历了一次迭代而没有继续;所以我希望对可能造成这种情况的事情有所了解。
完全披露:我将它从SQL Server移植到MySQL存储过程,这是我以前从未接受过的。 (意思是SQL Server,移植OR存储过程..)
WHILE @RowCnt <= @MaxRows DO
SELECT @currentReadSeq:=ReadSeq, @currentReadStrength:=ReadStrength, @currentReadDateTime:=ReadDateTime, @currentReaderID:=ReaderID FROM tblTempRead WHERE rownum = @RowCnt;
IF ( ((@lastReadSeq + 10) > @currentReadSeq) AND (@lastReaderId = @currentReaderId) ) THEN
SET @lastReadSeq = @currentReadSeq, @lastReadStrength = @currentReadStrength, @lastReadDateTime = @currentReadDateTime, @lastReaderID = @currentReaderID;
ELSE
INSERT INTO tblreaddataresults (SiteID, ReadDateTimeStart, ReadDateTimeEnd, ReadSeqStart, ReadSeqEnd, ReaderID, DirectSeconds) VALUES ('1002', @saveReadDateTime, @lastReadDateTime, @saveReadSeq, @lastReadSeq, @lastReaderID, timestampdiff(SECOND,@saveReadDateTime,@lastReadDateTime));
SET @saveReadSeq = @currentReadSeq, @saveReadStrength = @currentReadStrength, @saveReadDateTime = @currentReadDateTime, @saveReaderID = @currentReaderID;
SET @lastReadSeq = @saveReadSeq, @lastReadStrength = @saveReadStrength, @lastReadDateTime = @saveReadDateTime, @lastReaderID = @saveReaderID;
END IF;
SET @RowCnt = @RowCnt+1;
END WHILE;
答案 0 :(得分:2)
尝试此构造
WHILE (@RowCnt <= @MaxRows)
BEGIN
SELECT @currentReadSeq:=ReadSeq, @currentReadStrength:=ReadStrength, @currentReadDateTime:=ReadDateTime, @currentReaderID:=ReaderID FROM tblTempRead WHERE rownum = @RowCnt;
IF (((@lastReadSeq + 10) > @currentReadSeq) AND (@lastReaderId = @currentReaderId))
BEGIN
SET @lastReadSeq = @currentReadSeq, @lastReadStrength = @currentReadStrength, @lastReadDateTime = @currentReadDateTime, @lastReaderID = @currentReaderID;
END
ELSE
BEGIN
INSERT INTO tblreaddataresults (SiteID, ReadDateTimeStart, ReadDateTimeEnd,ReadSeqStart, ReadSeqEnd, ReaderID, DirectSeconds) VALUES ('1002',@saveReadDateTime, @lastReadDateTime, @saveReadSeq, @lastReadSeq, @lastReaderID,timestampdiff(SECOND,@saveReadDateTime,@lastReadDateTime));
SET @saveReadSeq = @currentReadSeq, @saveReadStrength = @currentReadStrength, @saveReadDateTime = @currentReadDateTime, @saveReaderID = @currentReaderID;
SET @lastReadSeq = @saveReadSeq, @lastReadStrength = @saveReadStrength,@lastReadDateTime = @saveReadDateTime, @lastReaderID = @saveReaderID;
END
SET @RowCnt = @RowCnt+1;
END