简体   繁体   English

mySQL 存储过程没有给我错误消息

[英]mySQL stored procedure not giving me error messages

The following stored procedure works as intended BUT if I force an error (see intentional type-o in INSERT statement) I don't get any error information.以下存储过程按预期工作,但是如果我强制出现错误(请参阅 INSERT 语句中的有意输入),我将不会收到任何错误信息。

I created a table called error_check and tried inserting error info into there to see if I could get some more information, but when there is an error, no rows are added to the table.我创建了一个名为 error_check 的表,并尝试在其中插入错误信息以查看是否可以获得更多信息,但是当出现错误时,表中不会添加任何行。 (When I take out the type-o, I get a new row with nulls in it, as expected.) (当我取出 type-o 时,正如预期的那样,我得到一个包含空值的新行。)

This used to work, so I'm sure it is something I added by mistake.这曾经有效,所以我确定这是我错误添加的内容。

CREATE DEFINER=`root`@`localhost` PROCEDURE `addClassToMasterList`( IN begin_date VARCHAR(10), IN day_num INT,
                               IN startTime VARCHAR(10), IN endTime VARCHAR (20), IN loc_id INT, IN studio VARCHAR(20),
                               IN class_name VARCHAR(45), IN owner_id INT,
                               IN club_id INT, OUT return_id VARCHAR(250))
BEGIN

DECLARE EXIT HANDLER FOR SQLEXCEPTION, NOT FOUND, SQLWARNING
BEGIN
    ROLLBACK;  
    GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE, 
                                @errno = MYSQL_ERRNO, 
                                @text = MESSAGE_TEXT;
    SET @display_error = CONCAT("ERROR ", @errno, " (", @sqlstate, "): ", @text);
END;   /*end of exception handing*/

SET @formatted_date = STR_TO_DATE(begin_date, '%m/%d/%Y');

/* Save all data as a new row in the Master Schedule */
INSERT INTO master_class_list_typeo_here
VALUES (null, 
    day_num, 
    @formatted_date, 
    null,
    startTime, 
    endTime, 
    loc_id, 
    studio,
    class_name, 
    owner_id, 
    club_id); 

SELECT LAST_INSERT_ID() INTO @new_id;

/* Put bits of error into a table so I can see what is going on. */
INSERT INTO error_check
VALUES (null, @display_error, @text, @errno);

/* If an error message was generated, return the error.
   Otherwise, return the new id that was automatically created. */
SELECT IF (@display_error, @display_error, @new_id) INTO return_id;
/* Also tried:  SELECT @display_error INTO return_id;  */

END

Try: 尝试:

...
GET STACKED DIAGNOSTICS CONDITION 1
...

13.6.7.3 GET DIAGNOSTICS Syntax 13.6.7.3获取诊断语法

... ...

The keyword STACKED means to retrieve information from the second diagnostics area, which is available only if the current context is a condition handler. 关键字STACKED意味着从第二诊断区域检索信息,仅当当前上下文是条件处理程序时才可用。

... ...

See db-fiddle . 参见db-fiddle

Place your INSERT INTO error_check statement within the BEGIN...END logical block of your exception handling, using INSERT INTO... SELECT... like so:INSERT INTO error_check语句放在异常处理的BEGIN...END逻辑块中,使用INSERT INTO... SELECT...像这样:

...
BEGIN
    -- ROLLBACK;  
    GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE, 
                                @errno = MYSQL_ERRNO, 
                                @text = MESSAGE_TEXT;
    SET @display_error = CONCAT("ERROR ", @errno, " (", @sqlstate, "): ", @text);

    /* Put bits of error into a table so I can see what is going on. */
    INSERT INTO error_check
    SELECT null, @display_error, @text, @errno;

END;   /*end of exception handing*/
...

This works for me.这对我有用。 Note that I also commented out the ROLLBACK请注意,我还注释掉了ROLLBACK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM