为什么我在存储过程中的事务没有打印出我想要的错误消息?

Why doesn’t my transaction inside of a stored procedure print the error message I want?

USE COLLEGE; 


DROP PROCEDURE IF EXISTS Fix_Egghead1;

DELIMITER $$

 CREATE PROCEDURE Fix_Egghead1()
BEGIN
    -- Setup error handing
    DECLARE errorOccurred INT DEFAULT FALSE;
    DECLARE errorMessage VARCHAR(255);

    -- Called when an error occurs 
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION 
    BEGIN 
        SET errorOccurred = TRUE;
        GET DIAGNOSTICS CONDITION 1
            errorMessage = MESSAGE_TEXT;

    END ;

       START TRANSACTION;

                 DELETE FROM Student
                 WHERE Student.ID= 57; 

           INSERT INTO Faculty(LastName, FirstName, Email, HireDate, Salary, DepartmentID) 
           VALUES('Egghead', 'Eduardo', 'EduardoEgghead@College.edu', 115000, 1);



       IF errorOccurred = TRUE THEN
        ROLLBACK;
        SELECT  CONCAT( 'The faculty member wasn't succesfully added to the Faculty table:' ,  errorMessage) AS Results; 
    ELSE
        COMMIT;
        SELECT 'The faculty member  was successfully added to the Faculty table.';
    END IF;

END
$$

DELIMITER ;

CALL Fix_Egghead1();

大家好,

我正在创建一个事务回滚,我在其中创建了一个存储过程,该过程输出教员是否已成功添加或未成功添加到教员 table,但问题是我的代码没有打印出它需要打印出来的错误信息。我知道这不是一次成功的交易,因为教员 Eduardo Egghead 仍然有学生的注册记录。我怎么解决这个问题?然而,我确实想出了一个解决方案,解决方案是摆脱 CONCAT 函数,只是简单地使用 SELECT 语句写入错误消息,就像我在提交部分所做的那样。还有其他建议吗?

从评论中移出:

我猜如果问题不是未转义的引号,那么就是 CONCAT() 试图连接 NULLCONCAT is to return NULL if any of its inputs is NULL. I'd suggest either using CONCAT_WS() or wrapping your variables in an IFNULL() 的行为:

SELECT CONCAT_WS(' ', "The faculty member wasn't succesfully added to the Faculty table:", errorMessage) AS Results;
-- or --
SELECT CONCAT("The faculty member wasn't succesfully added to the Faculty table: ", IFNULL(errorMessage,'NULL')) AS Results;