gpt4 book ai didi

MySQL存储过程问题。抛出警告。想不通为什么?

转载 作者:行者123 更新时间:2023-11-29 03:11:54 25 4
gpt4 key购买 nike

我有一个适用于申请人数据插入/更新操作的存储过程。更新操作工作正常。但是插入查询确实会出现 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 ;

最佳答案

关于MySQL存储过程问题。抛出警告。想不通为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6925800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com