How to throw an error in MySql procedure?

來源:互聯網
上載者:User

標籤:

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)BEGINDECLARE 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.

mysql stored-procedures throw stored-functions
shareimprove this question asked Feb 1 ‘11 at 13:06Arman1,89652952
 
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
add a comment
2 Answersactiveoldestvotes
up vote7down voteaccepted

Yes, there is: use the SIGNAL keyword.

shareimprove this answer edited Sep 2 ‘14 at 16:52Air3,75212446 answered Feb 1 ‘11 at 13:20Halasy861
 
2  
Thank you! DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET err= 1 exactly that what I need!!! – Arman Feb 2 ‘11 at 11:11
add a comment
up vote5down vote

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

CREATE PROCEDURE `raise`(`errno` BIGINT UNSIGNED, `message` VARCHAR(256))BEGINSIGNAL 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?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.