MySQL 错误处理例子[译]_Mysql

from http://www.devshed.com/c/a/MySQL/Error-Handling-Examples/
Error Handler Examples
Here are some examples of handler declarations:
If any error condition arises (other than a NOT FOUND ), continue execution after setting l_error=1 :
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET l_error=1;
If any error condition arises (other than a NOT FOUND ), exit the current block or stored program after issuing a ROLLBACK statement and issuing an error message:
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 'Error occurred – terminating';
END;
If MySQL error 1062 (duplicate key value) is encountered, continue execution after executing the SELECT statement (which generates a message for the calling program):
DECLARE CONTINUE HANDER FOR 106 2
SELECT 'Duplicate key in index';
If SQLSTATE 23000 (duplicate key value) is encountered, continue execution after executing the SELECT statement (which generates a message for the calling program):
DECLARE CONTINUE HANDER FOR SQLSTATE '23000'
SELECT 'Duplicate key in index';
When a cursor fetch or SQL retrieves no values, continue execution after setting l_done=1 :
DECLARE CONTINUE HANDLER FOR NOT
FOUND
SET l_done=1;
Same as the previous example, except specified using a SQLSTATE variable rather than a named condition:
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000 '
SET l_done=1;
Same as the previous two examples, except specified using a MySQL error code variable rather than a named condition or SQLSTATE variable:
DECLARE CONTINUE HANDLER FOR 1329
SET l_done=1;

错误处理例子
有几种错误处理的声明形式:
§ 如果任何错误(不是 NOT FOUND ) , 设置 l_error 为 1 后继续执行:
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET l_error=1;
§ 如果发生任何错误(不是 NOT FOUND), 执行 ROLLBACK和产生一条错误消息后退出当前块或存储过程。
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 'Error occurred – terminating';
END;
§ 如果 MySQL 1062错误 (重复的健值 )发生,执行 SELECT语句(向调用程序发一条消息)后继续执行
DECLARE CONTINUE HANDER FOR 106 2
SELECT 'Duplicate key in index';
§ 如果 SQLSTATE 2300错误 (重复的健值 )发生,执行 SELECT语句(向调用程序发一条消息)后继续执行
DECLARE CONTINUE HANDER FOR SQLSTATE '23000'
SELECT 'Duplicate key in index';
§ 当游标或者 SQL 选择语句没有返回值时,设置 l_done=1 后继续执行
DECLARE CONTINUE HANDLER FOR NOT
FOUND
SET l_done=1;
§ 此例除了用 SQLSTATE 变量而不是命名条件以外,跟前一个例子一样
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000 '
SET l_done=1;
§ 此例除了用 MySQL 的错误码变量而不是命名条件或者 SQLSTATE 变量以外,跟前两个例子一样
DECLARE CONTINUE HANDLER FOR 1329
SET l_done=1;

时间: 2024-10-21 17:58:11

MySQL 错误处理例子[译]_Mysql的相关文章

MYSQL错误:Can’t open file: ‘×××.MYI’ (errno: 145)修复方法_Mysql

减少此类问题发生的办法就是尽量减少服务器无故断电,关闭服务器时,最后先手动关闭mysql数据库. 下面看看这个问题: Can't open file: '×××.MYI' (errno: 145),这个错误一般就是你的数据库表文件损坏造成的,造成的原因大概是你把数据库文件挪来挪去的结果,当然不排除其他原因啊. 具体数据库出现错误的提示代码是: Invalid SQL: -- MySQL 错误!: 1016 (Can't open file: '×××.MYI' (errno: 145)) 解决这

MySQL 错误errors 处理例子

Error Handler Examples Here are some examples of handler declarations: If any error condition arises (other than a NOT FOUND ), continue execution after setting l_error=1 : DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET l_error=1; If any error conditi

Mysql 错误too many connections解决方案_Mysql

Mysql 错误提示too many connections,最近遇到这个错误,经过上网查资料解决了,这里记录下,帮助有需要的朋友, 解决方法是修改/etc/mysql/my.cnf,添加以下一行: set-variable = max_connections=500 或在启动命令中加上参数 max_connections=500 就是修改最大连接数,然后重启mysql.默认的连接数是100,太少了,所以容易出现如题错误. 以下是mysql.com网站的相关说明: If you get a To

Ubuntu下启动、停止、重启MySQL,查看错误日志命令大全_Mysql

1)启动: sudo /etc/init.d/mysql start 2)停止: sudo /etc/init.d/mysql stop 3)重启: sudo /etc/init.d/mysql restart 4)查看日志: cat /var/log/mysql.err cat /var/log/mysql/error.log 5)不支持中文 修改 /etc/mysql/my.cnf 在 [client] 下面增加: default-character-set=utf8 或 character

解决mySQL中1862(phpmyadmin)/1820(mysql)错误的方法_Mysql

发现问题 之前一直运行的好好的,突然mysql就无法工作了.请求命令后报错误:ERROR 1820 (HY000): You must SET PASSWORD before executing this statement ,使用phpMyAdmin管理工具也无法登录,显示错误:#1862 无法登录 MySQL 服务器 错误解决 进入mysql的服务器中使用mysql指令进行登录及操作. 1.登录mysql:mysql -uroot -proot (root是我的帐号及密码) 2.尝试是否报1

mysql错误处理之ERROR 1786 (HY000)_Mysql

ERROR 1786 (HY000) [环境描述] msyql5.6.14 [报错信息] 执行create table ... select的时候遇到报错: 复制代码 代码如下: db1 [test] [23:01:58]> create tablelgmnr_bak select * from lgmnr; ERROR 1786 (HY000): CREATE TABLE ... SELECTis forbidden when @@GLOBAL.ENFORCE_GTID_CONSISTENCY

MySQL 1067错误解决方法集合_Mysql

1  安装MYSQL后更改了ROOT的密码后用  net startmysql  启动时我就遇到了这样的问题.使用以下命令后  c:\mysql\bin\mysqladmin-u root -p shutdown  再net start mysql就没有这个错误提示了!  *************  2  MySQL的1067错误  Q:我的Mysql碰到了 1067 错误  错误信息为:  A system error has occurred.  System error 1067 has

mysql错误处理之ERROR 1665 (HY000)_Mysql

ERROR 1665 (HY000) [环境描述] msyql5.6.14 [报错信息] 执行SQL语句的时候报错: ERROR 1665 (HY000): Cannot executestatement: impossible to write to binary log since BINLOG_FORMAT = STATEMENTand at least one table uses a storage engine limited to row-based logging.InnoDB

MYSQL错误:Can’t open file: ‘×××.MYI’ (errno: 145)

这个错误是典型的mysql表受损造成的,解决的办法就是修复表,这个问题也是mysql经常容易出现的,mysql表和索引损坏的几率很大,但是修复也很方便,减少此类问题发生的办法就是尽量减少服务器无故断电,关闭服务器时,最后先手动关闭mysql数据库. 下面看看这个问题: Can't open file: '×××.MYI' (errno: 145),这个错误一般就是你的数据库表文件损坏造成的,造成的原因大概是你把数据库文件挪来挪去的结果,当然不排除其他原因啊. 具体数据库出现错误的提示代码是: I