mysql 定义条件和处理程序 demo解析

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 表时,会因为主键唯一造成错误;

猜你喜欢

转载自blog.csdn.net/weixin_40021744/article/details/87745138