如何在 MySql 过程中抛出错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4862911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:37:06  来源:igfitidea点击:

How to throw an error in MySql procedure?

mysqlstored-proceduresthrowstored-functions

提问by Arman

What is the mechanism to force the MySQL to throw an error within the stored procedure?

强制 MySQL 在存储过程中抛出错误的机制是什么?

I have a procedure which call s another function:

我有一个调用另一个函数的过程:

PREPARE my_cmd FROM @jobcommand;
EXECUTE my_cmd;
DEALLOCATE PREPARE my_cmd;

the job command is:

作业命令是:

jobq.exec("Select 1;wfdlk# to simulatte an error");

then:

然后:

CREATE PROCEDURE jobq.`exec`(jobID VARCHAR(128),cmd TEXT)
BEGIN
DECLARE result INT DEFAULT 0;  
SELECT sys_exec( CONCAT('echo ',cmd,' |  base64 -d > ', '/tmp/jobq.',jobID,'.sh ; bash /tmp/jobq.',jobID,'.sh &> /tmp/jobq.',jobID)) INTO result; 
IF result>0 THEN 
# call raise_mysql_error(result); 
END IF;
END;

My jobq.execis always succeeding. Are there way to rise an error? How to implement raise_mysql_error function??

我的工作q。exec总是成功。有没有办法提高错误?如何实现 raise_mysql_error 函数??

BTW I am using MySQL 5.5.8

顺便说一句,我正在使用 MySQL 5.5.8

thanks Arman.

谢谢阿尔曼。

采纳答案by Halasy

Yes, there is: use the SIGNALkeyword.

是的,有:使用SIGNAL关键字。

回答by BlitZ

You may use following stored procedure to emulate error-throwing:

您可以使用以下存储过程来模拟错误抛出:

CREATE PROCEDURE `raise`(`errno` BIGINT UNSIGNED, `message` VARCHAR(256))
BEGIN
SIGNAL SQLSTATE
    'ERR0R'
SET
    MESSAGE_TEXT = `message`,
    MYSQL_ERRNO = `errno`;
END

Example:

例子:

CALL `raise`(1356, 'My Error Message');