Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Gespeicherte MySQL-Prozedur mit IF/THEN/ELSE

Versuchen Sie dieses Konstrukt

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