12

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??

I am using MySQL 5.5.8

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Arman
  • 4,566
  • 10
  • 45
  • 66

2 Answers2

9

Yes, there is: use the SIGNAL keyword.

Air
  • 8,274
  • 2
  • 53
  • 88
Halasy
  • 116
  • 1
  • 3
    Thank you! DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET err= 1 exactly that what I need!!! – Arman Feb 02 '11 at 11:11
7

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');
BlitZ
  • 12,038
  • 3
  • 49
  • 68