美文网首页
MySql 使用游标遇到的问题

MySql 使用游标遇到的问题

作者: lbyang | 来源:发表于2020-08-05 09:12 被阅读0次
    使用定义的变量,不能和字段名称一样包括大小写

    下面的定义的变量中 ID CAR_CODE就是错误的,出现的情况是无法读取值

    错误

    BEGIN
        DECLARE GPS_ID VARCHAR(255) DEFAULT('');
        DECLARE ID VARCHAR(255) DEFAULT('');
        DECLARE CAR_CODE VARCHAR(255) DEFAULT('');
        DECLARE iCursor CURSOR FOR SELECT id,car_code FROM table where  state=0 and trans_type=0;
        DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET CAR_CODE = NULL;
        OPEN iCursor;
        FETCH iCursor INTO ID,CAR_CODE;
        WHILE (CAR_CODE IS NOT NULL) DO
            SELECT gps_id INTO GPSID FROM table WHERE car_code = CAR_CODE;
            UPDATE table SET cache_key=CONCAT('finish_',GPS_ID) WHERE id=ID;
            FETCH iCursor INTO ID,CAR_CODE;
        END WHILE;
        CLOSE iCursor;
        
    END
    

    正确

    BEGIN
        DECLARE TEMP_GPS_ID VARCHAR(255) DEFAULT('');
        DECLARE TEMP_ID VARCHAR(255) DEFAULT('');
        DECLARE TEMP_CAR_CODE VARCHAR(255) DEFAULT('');
        DECLARE iCursor CURSOR FOR SELECT id,car_code FROM table where  state=0 and trans_type=0;
        DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET TEMP_CAR_CODE = NULL;
        OPEN iCursor;
        FETCH iCursor INTO TEMP_ID,TEMP_CAR_CODE;
        WHILE (TEMP_CAR_CODE IS NOT NULL) DO
            SELECT gps_id INTO TEMP_GPSID FROM table WHERE car_code = TEMP_CAR_CODE;
            UPDATE table SET cache_key=CONCAT('finish_',TEMP_GPS_ID) WHERE id=TEMP_ID;
            FETCH iCursor INTO TEMP_ID,TEMP_CAR_CODE;
        END WHILE;
        CLOSE iCursor;
        
    END
    

    相关文章

      网友评论

          本文标题:MySql 使用游标遇到的问题

          本文链接:https://www.haomeiwen.com/subject/ykknrktx.html