[MySQL光速入门]017 存储过程当中的"异常处理"

异常处理

若是出现报错了, 怎么办sql

搁在之前, 直接就停了post

有了异常处理, 咱们能够选择继续仍是终止ui

create table test(id int);
create table test(id int);
select 1+1;
复制代码

这段代码会报错(1050), 由于连续建立了两个相同的表...spa

create table test(id int)
> 1050 - Table 'test' already exists
> 时间: 0.003s
复制代码

因此select 1+1;不会执行, 咱们也看不到2...code

如今咱们有两种选择get

  1. 忽略错误, 继续执行, 你会看到2
  2. 终止sql语句, 可是不要报错

continue 跳过错误string

drop PROCEDURE if EXISTS hello;

create procedure hello() begin 
declare existed condition for 1050;
declare continue handler for existed set @fail = 1;
create table teacher(id int);
select 1+1;
end;

call hello();
复制代码

也能够简写it

drop PROCEDURE if EXISTS hello;

create procedure hello() begin 
declare continue handler for 1050 set @fail = 1;
create table teacher(id int);
select 1+1;
end;

call hello();
复制代码

exit 终止程序io

drop PROCEDURE if EXISTS hello;

create procedure hello() begin 
declare existed condition for 1050;
declare exit handler for existed set @fail = 1;
create table teacher(id int);
select 1+1;
end;

call hello();
复制代码

整行语句会由于重复建表而终止, 也不会输出2, 可是不会报错 也能够简写成入门

drop PROCEDURE if EXISTS hello;

create procedure hello() begin 
declare exit handler for 1050 set @fail = 1;
create table teacher(id int);
select 1+1;
end;

call hello();
复制代码

快速跳转

相关文章
相关标签/搜索