How to throw an error in MySql procedure?

2018-06-17 23:01:39来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

9down votefavorite
 

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
 
1  
related : stackoverflow.com/questions/465727/… – Haim Evgi Feb 1 '11 at 13:09
    
also read this chapter docstoc.com/docs/687360/Error-Handling-In-Stored-Procedure – Haim Evgi Feb 1 '11 at 13:11

2 Answers

activeoldestvotes
up vote7down voteaccepted

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
up vote5down vote

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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Cursors in MySQL Stored Procedures

下一篇:学习笔记:腾讯云——服务器mysql操作