How to throw an error in MySql procedure?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了How to throw an error in MySql procedure?相关的知识,希望对你有一定的参考价值。

http://stackoverflow.com/questions/4862911/how-to-throw-an-error-in-mysql-procedure

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

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.exec is always succeeding. Are there way to rise an error? How to implement raise_mysql_error function??

BTW I am using MySQL 5.5.8

thanks Arman.

shareimprove this question
 

2 Answers

Yes, there is: use the SIGNAL keyword.

shareimprove this answer
 
2  
Thank you! DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET err= 1 exactly that what I need!!! – Arman Feb 2 ‘11 at 11:11
技术分享

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‘);

以上是关于How to throw an error in MySql procedure?的主要内容,如果未能解决你的问题,请参考以下文章

How to click on a point on an HTML5 canvas in Python selenium webdriver

How to generate an SSH key pair in Windows using PuTTY?

How to run an manually installed program from terminals in Linux / Ubuntu

How to Check if an Array Contains a Value in Java Efficiently?

Think in ISP(image sensor pipe )之How to implement an effecitive AE

How to Fix Error Code 0x800700ea in Windows 10