首页 > 代码库 > 条件异常处理
条件异常处理
mysql> call actor_insert();
ERROR 1062 (23000): Duplicate entry ‘1‘ for key ‘PRIMARY‘
A numeric error code (1146
). This number is MySQL-specific and is not portable to other database systems.
A five-character SQLSTATE value (‘
23000‘
). The values are taken from ANSI SQL and ODBC and are more standardized.
--标准错误码捕获
DECLARE CONTINUE HANDLER FOR SQLSTATE ‘23000‘ SET @x2 = 1;
--捕获 mysql-error-code:
DECLARE CONTINUE HANDLER FOR 1062 SET @x2 = 1;
--事先定义 condition_name:
DECLARE DuplicateKey CONDITION FOR SQLSTATE ‘23000‘;
DECLARE CONTINUE HANDLER FOR DuplicateKey SET @x2 = 1;
--捕获 SQLEXCEPTION
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET @x2 = 1;
·SQLWARNING 是对所有以 01 开头的 SQLSTATE 代码的速记。
·NOT FOUND 是对所有以 02 开头的 SQLSTATE 代码的速记。
·SQLEXCEPTION 是对所有没有被 SQLWARNING 或 NOT FOUND 捕获的 SQLSTATE 代码的速记
条件异常处理