How to throw an error in MySql procedure?

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.

mysql stored-procedures throw stored-functions


shareimprove this question

asked Feb 1 ‘11 at 13:06

Arman

1,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 Answers

activeoldestvotes


up vote7down voteaccepted

Yes, there is: use the SIGNAL keyword.


shareimprove this answer

edited Sep 2 ‘14 at 16:52

Air

3,75212446

answered Feb 1 ‘11 at 13:20

Halasy

861

 

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))
BEGIN
SIGNAL SQLSTATE
    ‘ERR0R‘
SET
    MESSAGE_TEXT = `message`,
    MYSQL_ERRNO = `errno`;
END

Example:

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

时间: 2025-01-02 03:16:36

How to throw an error in MySql procedure?的相关文章

Error: Table "mysql"."innodb_table_stats" not found.

Error: Table "mysql"."innodb_table_stats" not found. 2013-04-02 09:07:35 7fc53f5e4700 InnoDB: Recalculation of persistent statistics requested for table "mydatabase"."mytable" but the required persistent statistics

InnoDB: Error: Table "mysql"."innodb_table_stats" not found.

1,Mysqldump的时候报错如下: 2014-05-05 14:12:37 7f004a9a2700 InnoDB: Error: Table "mysql"."innodb_table_stats" not found. 但是show tables我看这个表示存在的: 但是show create table innodb_index_stats;报错如下: mysql> show create table innodb_index_stats; ERRO

MySql Table错误:is marked as crashed and last (automatic?) 和 Error: Table "mysql"."innodb_table_stats" not found

一.mysql 执行select 的时候报Table错误:is marked as crashed and last (automatic?) 解决方法如下: 找到mysql的安装目录的bin/myisamchk工具,在命令行中输入: myisamchk -c -r ../data/dedecmsv4/dede_archives.MYI 然后myisamchk 工具会帮助你恢复数据表的索引.重新启动mysql,问题解决. 问题分析: 1.错误产生原因,有网友说是频繁查询和更新dede_archi

InnoDB: Error: Table "mysql"."innodb_table_stats" not found索引表没找到,主从停止

InnoDB: Error: Table "mysql"."innodb_table_stats" not found. 导致:Seconds_Behind_Master: 27360  mysql主从同步停止 stop slave; 会一直卡住,service mysqld restart后,马上又会停止同步,出现同样问题. 初步判断: 因为innodb_table_stats表丢失,导致主从同步出现问题. 解决办法: 新建innodb_index_stats/i

jdbc 处理mysql procedure返回的多个结果集

1:测试数据库表user mysql> desc user$$ +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_in

Lost connection to MySQL server at ‘reading initial communication packet', system error: 0 mysql远程连接问题

在用Navicat for MySQL远程连接mysql的时候,出现了 Lost connection to MySQL server at ‘reading initial communication packet', system error: 0 好恶心,服务器重启了一次,就一上午都是在处理mysql的各种问题. 第一步:找到:my.cnf文件 其实我也不知道在哪里,用指令帮我们找吧. find / -name my.cnf 找到了后. 第二步:vi my.cnf vi my.cnf 在m

fedota 20编译php checking for mysql_set_server_option in -lmysqlclient... no configure: error: wrong mysql library version or lib not found. 错误

php-5.5.15 + mysql-5.5.27,编译php的时候报错,checking for mysql_set_server_option in -lmysqlclient... no configure: error: wrong mysql library version or lib not found. Check config.log for more information. 解决方法重新编译mysql,并把-DENABLE_DTRACE=0这项加进mysql的编译cmake

mysql数据库二进制初始化出现:170425 17:47:04 [ERROR] /application/mysql//bin/mysqld: unknown option '--skip-locking' 170425 17:47:04 [ERROR] Aborting 解决办法

[[email protected] mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/application/mysql/ --datadir=/application/mysql/data/Installing MySQL system tables...170425 17:47:04 [ERROR] /application/mysql//bin/mysqld: unknown option '--skip-locking

Mysql innodb错误解决 InnoDB: Error: table `mysql`.`innodb_table_stats`

通过ELK监控发现,程序连接mysql DB 失败,通过看程序的log和mysql的error log发现mysql中出现error 查看Mysql日志 发现 InnoDB: Error: table `mysql`.`innodb_table_stats` does not exist in the InnoDB internal 这个原因很明显 ,是mysql库的innodb_table_stats表损坏了. 首先登录mysql查看表是否存在?   结果:存在的. Tables_in_mys