开发者

MySQL Stored Procedure with IF/THEN/ELSE

开发者 https://www.devze.com 2023-03-21 04:56 出处:网络
I have a MySQL stored procedure and in it, the following WHILE statement. I have confirmed that @RowCnt is 1, and @MaxRows is 6090, however after further debugging, I realized that the WHILE statemen

I have a MySQL stored procedure and in it, the following WHILE statement.

I have confirmed that @RowCnt is 1, and @MaxRows is 6090, however after further debugging, I realized that the WHILE statement is going through a single iteration and not continuing; so I'm hoping to have some light shed on what could possibly be causing this.

Fu开发者_开发技巧ll disclosure: I ported this from SQL Server to a MySQL stored procedure, something I have never taken on before. (meaning SQL Server, porting OR stored procedures..)

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;


Try This Construct

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
0

精彩评论

暂无评论...
验证码 换一张
取 消