MySQL存储过程问题。引发警告。不知道为什么?

时间:2011-08-03 11:36:01

标签: mysql stored-procedures error-handling warnings

我有一个适用于申请人数据插入/更新操作的存储过程。更新操作可以正常工作。但插入查询确实将errorFlag设为2,表示警告。我只是想弄清楚警告是什么。

这是我的SP。

DELIMITER $$

DROP PROCEDURE IF EXISTS mydb.insertApplicant $$

CREATE PROCEDURE mydb.insertApplicant(

                                    IN appFirstName VARCHAR(128),
                                    IN appLastName VARCHAR(128),
                                    IN appEmail VARCHAR(128),
                                    IN appPhone VARCHAR(16),
                                    IN appZipcode VARCHAR(16),
                                    IN appCity VARCHAR(32),
                                    IN appState VARCHAR(32),
                                    IN appCountry VARCHAR(32))
    MODIFIES SQL DATA
BEGIN

    DECLARE appAction VARCHAR(16) DEFAULT NULL;
    DECLARE applicantId INTEGER DEFAULT 0;
    DECLARE updateFlag INTEGER DEFAULT 0;
    DECLARE errorFlag INTEGER DEFAULT 0;
    DECLARE errorQuery VARCHAR(1024) DEFAULT NULL;

    /* Set continue handler for sql exception */
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
    BEGIN
        SET errorFlag = 1;
    END;

    /* Set continue handler for sql warning */
    DECLARE CONTINUE HANDLER FOR SQLWARNING
    BEGIN
        SET errorFlag = 2;
    END;

    /* Turn auto-commit off */
    SET AUTOCOMMIT = 0;

    /* Start ttsnsaction */
    START TRANSACTION;

    /* Set savepoint. If an error occurs, we will rollback to this point */
    SAVEPOINT Savepoint_S1;

    MAIN_BLOCK:BEGIN

        /* Check if applicant already exists */
        SELECT applicant_id INTO applicantId FROM tblApplicant WHERE email = appEmail;

        /* If applicant already exists, then update applicant details */
        IF (applicantId) THEN

            SET appAction = 'Update';

            /* Update applicant details */
            SET @sqlQuery = CONCAT("UPDATE tblApplicant SET ",
            "first_name = '", appFirstName, "', last_name = '", appLastName,
            "', update_date = NOW(), phone = '", appPhone, "', zipcode = '", appZipcode,
            "', city = '", appCity, "', state = '", appState, 
            "', country = '", appCountry, "' WHERE applicant_id = '", applicantId, "'");

            PREPARE stmt FROM @sqlQuery;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;

            /* Set update flag to true */
            SET updateFlag = 1;

        END IF;

        IF(updateFlag <> 1) THEN

            SET appAction = 'Insert';

            /* insert applicant details */
            SET @sqlQuery = CONCAT("INSERT INTO tblApplicant (first_name, last_name, email, reg_date, ",
            "phone, zipcode, city, state, country) VALUES ('",
            appFirstName, "', '", appLastName, "', '", appEmail, "', NOW(), '",
        appPhone, "', '", appZipcode, "', '", appCity, "', '", appState, "', '", appCountry, "')");

            PREPARE stmt FROM @sqlQuery;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;

            /* Check if an error has occured */
            IF (errorFlag = 0) THEN
                SELECT LAST_INSERT_ID() INTO applicantId;
            END IF;

        END IF;

        /* Check if an error has occured */
        IF errorFlag THEN
            ROLLBACK TO Savepoint_S1;
            SELECT @sqlQuery INTO errorQuery;
            LEAVE MAIN_BLOCK;
        END IF;

    END; /* MAIN_BLOCK ends here */

    /* Commit changes if any */
    COMMIT;

    /* Turn auto-commit ON */
    SET AUTOCOMMIT = 1;

    /* Select paramters */
    SELECT errorFlag, errorQuery, appAction, applicantId;

END $$

DELIMITER ;

1 个答案:

答案 0 :(得分:1)