I have a stored procedure that works for applicant data insert/update actions. Update actions do work fine. But insert queries do come up with errorFlag as 2, which indicates a warning. I just cant figure out what the warning is.
Here is my 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 ;
Use GET DIAGNOSTICS
See http://dev.mysql.com/doc/refman/5.6/en/get-diagnostics.html
精彩评论