create table test.t( s1 int, primary key(s1));
create procedure handlerdemo()
begin
declare continue handler for SQLSTATE '23000' SET @x2=1;
set @x=1;
insert into test.t values(1);
set @x=2;
insert into test.t values(1);
set @x=3;
end;
解析
首先创建了 Table t , 其中 si 是主键,不可重复;
对于 SQLSTATE 设置了 CONTINUE(遇到错误不处理,继续执行)。如果遇到了错误就会将 @x2 设置为 1;
在我们第二次插入数据 1 到 test.t 表时,会因为主键唯一造成错误;