当前位置:Gxlcms > 数据库问题 > mysql触发器语法的一个实例

mysql触发器语法的一个实例

时间:2021-07-01 10:21:17 帮助过:8人阅读

第一次写的触发器例如以下:

CREATE TRIGGER istmingxi 
AFTER UPDATE ON sys_shenbao FOR EACH ROW BEGIN 
SELECT @kid:=jl.kid,@gonghao:=shen.gonghao,@vuid:=jl.vuid,@zhibiao:=ct.leibie,
@citiao:=ct.bianhao,@fenshu:=ct.fenshu,@bumen:=zl.bumen,@ctime:=shen.verifytime
FROM sys_shenbao shen
INNER JOIN sys_shenbjl jl ON jl.d = shen.sid
INNER JOIN sys_citiao ct ON ct.bianhao = shen.ctbh
INNER JOIN sys_jibzl zl ON zl.gonghao = shen.gonghao
WHERE shen.sid = new.sid;
if fenshu > 0 then
set @jiafen = fenshu;
set @jianfen = 0;
else
set @jiafen = 0;
set @jianfen = fenshu;
end if;
INSERT INTO sys_mingxi(`kid`,`gonghao`,`vuid`,`zhibiao`,`bumen`,`jiafen`,`jianfen`,`yue`,`ctime`)
VALUES(kid,gonghao,vuid,zhibiao,citiao,bumen,jiafen,jianfen,0,ctime);
END;

生成上面触发器时报错:Not allowed to return a result set from a trigger;原因是trigger 中不同意返回select出来的结果集。所以 “select @kid:=jl.kid....”部分改变成还有一种写法,例如以下

CREATE TRIGGER istmingxi 
AFTER UPDATE ON sys_shenbao FOR EACH ROW BEGIN 
SELECT jl.skid into @kid, shen.gonghao into @gonghao,jl.vuid into @vuid,ct.leibie into @zhibiao,
ct.bianhao into @citiao,ct.fenshu into @fenshu,zl.bumen into @bumen,shen.verifytime into @ctime
FROM sys_shenbao shen
INNER JOIN sys_shenbjl jl ON jl.id = shen.sid
INNER JOIN sys_citiao ct ON ct.bianhao = shen.ctbh
INNER JOIN sys_jibzl zl ON zl.gonghao = shen.gonghao
WHERE shen.sid = new.sid;
if fenshu > 0 then
set @jiafen = fenshu;
set @jianfen = 0;
else
set @jiafen = 0;
set @jianfen = fenshu;
end if;
INSERT INTO sys_mingxi(`kid`,`gonghao`,`vuid`,`zhibiao`,`bumen`,`jiafen`,`jianfen`,`yue`,`ctime`)
VALUES(kid,gonghao,vuid,zhibiao,citiao,bumen,jiafen,jianfen,0,ctime);
END;

此时还是会报错:SQL 错误 [1327] [42000]: Undeclared variable: shen; Undeclared variable: shen;原因是“select jl.skic into @kid...”部分的select语句的语法还是不正确。

原因是:欲想通过一个SELECT语句对多个变量赋值。请採用下面语句形式:SELECT id,name INTO @v1,@v2 FROM ...

所以终于我改成了例如以下的方式:

CREATE TRIGGER istmingxi 
AFTER UPDATE ON sys_shenbao FOR EACH ROW BEGIN 
SELECT jl.skid,shen.gonghao,jl.vuid,ct.leibie,ct.bianhao,ct.fenshu,zl.bumen,shen.verifytime
INTO @kid,@gonghao,@vuid,@zhibiao,@citiao,@fenshu,@bumen,@ctime
FROM sys_shenbao shen
INNER JOIN sys_shenbjl jl ON jl.id = shen.sid
INNER JOIN sys_citiao ct ON ct.bianhao = shen.ctbh
INNER JOIN sys_jibzl zl ON zl.gonghao = shen.gonghao
WHERE shen.sid = new.sid;
if fenshu > 0 then
set @jiafen = fenshu;
set @jianfen = 0;
else
set @jiafen = 0;
set @jianfen = fenshu;
end if;
INSERT INTO sys_mingxi(`kid`,`gonghao`,`vuid`,`zhibiao`,`bumen`,`jiafen`,`jianfen`,`yue`,`ctime`)
VALUES(kid,gonghao,vuid,zhibiao,citiao,bumen,jiafen,jianfen,0,ctime);
END;

到这执行的时候还是会报错:ERROR 1172 (42000): Result consisted of more than one row。

这个报错的原因是select into返回了多行记录,这在给变量赋值的时候是不同意的,后来检查是where条件的问题;

改动了where条件后返回一条记录这个问题攻克了。

再次执行时又报错:SQL 错误 [1054] [42S22]: Unknown column ‘fenshu‘ in ‘field list‘ ;这个错误的原因是以下在引用变量“fenshu”时语法错误;

上面定义变量时用了@符号,以下引用时也要用上@符号。

最后触发器改成例如以下:

AFTER UPDATE ON sys_shenbao FOR EACH ROW BEGIN 
if new.verifystatus = 2 then
SELECT jl.skid,shen.gonghao,jl.vuid,ct.leibie,ct.bianhao,ct.fenshu,zl.bumen,shen.verifytime
INTO @kid,@gonghao,@vuid,@zhibiao,@citiao,@fenshu,@bumen,@ctime
FROM sys_shenbao shen
INNER JOIN sys_shenbjl jl ON jl.id = shen.sid
INNER JOIN sys_citiao ct ON ct.bianhao = shen.citbh
INNER JOIN sys_jibzl zl ON zl.gonghao = shen.gonghao
WHERE shen.id = new.id;
if @fenshu > 0 then
set @jiafen = @fenshu;
set @jianfen = 0;
else
set @jiafen = 0;
set @jianfen = @fenshu;
end if;
INSERT INTO sys_mingxi(`kid`,`gonghao`,`vuid`,`zhibiao`,`citiao`,`bumen`,`jiafen`,`jianfen`,`yue`,`ctime`)
VALUES(@kid,@gonghao,@vuid,@zhibiao,@citiao,@bumen,@jiafen,@jianfen,0,@ctime);
end if;
END;

到这触发器成功了。


最后总结例如以下:

1、trigger 中不同意返回select出来的结果集

2、欲想通过一个SELECT语句对多个变量赋值。请採用下面语句形式:SELECT id,name INTO @v1,@v2 FROM ...

3、select into给变量赋值的语法不同意返回多行

4、定义变量时用了@符号,以下引用时也要用上@符号



mysql触发器语法的一个实例

标签:track   data-   ace   style   mysql   ack   res   bsp   indent   

人气教程排行