MySQL database - stored procedure - condition handler (specify the specific status code through SQLSTATE, and NOT FOUND through the code abbreviation of SQLSTATE)

Table of contents

introduce

Case

Specify specific status codes through SQLSTATE

NOT FOUND via SQLSTATE code abbreviation


introduce

Conditional handlers (Handler) can be used to define corresponding processing steps when problems are encountered during the execution of the process control structure. The specific syntax is:

DECLARE handler_action HANDLER FOR condition_value [, condition_value]
... statement ;

handler_action 的取值:
    CONTINUE: 继续执行当前程序
    EXIT: 终止执行当前程序

condition_value 的取值:
    SQLSTATE sqlstate_value: 状态码,如 02000
    SQLWARNING: 所有以01开头的SQLSTATE代码的简写
    NOT FOUND: 所有以02开头的SQLSTATE代码的简写
    SQLEXCEPTION: 所有没有被SQLWARNING 或 NOT FOUND捕获的SQLSTATE代码的简写

The bug we encountered in the previous article can be solved through the condition handler.

Case

We continue to complete the requirements raised in the previous part and solve the problems.

According to the incoming parameter uage, query the name of all users whose age is less than or equal to uage in the user table tb_user
(name) and profession (profession), and add the user The name and profession are inserted into a new table
(id,name,profession) created.

  • Specify specific status codes through SQLSTATE

-- 逻辑:
-- A. 声明游标, 存储查询结果集
-- B. 准备: 创建表结构
-- C. 开启游标
-- D. 获取游标中的记录
-- E. 插入数据到新表中
-- F. 关闭游标

create procedure p(in uage int)
begin

    declare uname varchar(100);
    declare upro varchar(100);
    declare u_cursor cursor for select name,profession 
        from tb_user where age <= uage;

-- 声明条件处理程序 : 当SQL语句执行抛出的状态码为02000时,
-- 将关闭游标u_cursor,并退出
    declare exit handler for SQLSTATE '02000' close u_cursor;

    drop table if exists tb_user_pro;

    create table if not exists tb_user_pro(
        id int primary key auto_increment,
        name varchar(100),
        profession varchar(100)
    );

    open u_cursor;
    
    while true do
    
        fetch u_cursor into uname,upro;
        insert into tb_user_pro values (null, uname, upro);

    end while;

    close u_cursor;

end;

call p(30);
  • NOT FOUND via SQLSTATE code abbreviation

Status code starting with 02, the code abbreviation is NOT FOUND

create procedure p(in uage int)
begin

    declare uname varchar(100);
    declare upro varchar(100);
    declare u_cursor cursor for select name,profession 
        from tb_user where age <= uage;

-- 声明条件处理程序 : 当SQL语句执行抛出的状态码为02开头时,
-- 将关闭游标u_cursor,并退出

    declare exit handler for not found close u_cursor;

    drop table if exists tb_user_pro;

    create table if not exists tb_user_pro(
        id int primary key auto_increment,
        name varchar(100),
        profession varchar(100)
    );

    open u_cursor;

    while true do

        fetch u_cursor into uname,upro;
        insert into tb_user_pro values (null, uname, upro);
    
    end while;

    close u_cursor;

end;

call p(30);

For specific error status codes, please refer to the official documentation:
https://dev.mysql.com/doc/refman/8.0/en/declare-handler.html
https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html

Guess you like

Origin blog.csdn.net/li13437542099/article/details/134406937