我正在尝试创建一个触发器,但它显示“不允许显式和隐式命令”。我试图在这里提及其他主题,但我不清楚。我的触发代码。
USE `vms`;
DELIMITER $$
CREATE TRIGGER `trg_bookingdetails` AFTER INSERT ON `bookingdetails` FOR EACH ROW
BEGIN
declare i int default 1;
DECLARE v_ga INT DEFAULT 0;
declare v_bid varchar(20);
declare v_bdate date;
declare v_sdate date;
declare v_sid varchar(20);
declare v_bcode varchar(10);
declare v_q int;
declare v_vid int;
declare v_jj int;
declare v_ss int;
declare v_mj int;
create temporary table if not exists temp_boo
(
`BID` VARCHAR(50) NULL DEFAULT NULL,
`BDate` DATE NULL DEFAULT NULL,
`SDate` DATE NULL DEFAULT NULL,
`SID` VARCHAR(20) NULL DEFAULT NULL,
`BCode` VARCHAR(20) NULL DEFAULT NULL,
`VID` VARCHAR(20) NULL DEFAULT NULL
);
begin
select bm.BID,bm.BDate,bm.SDate,bm.SID,bd.BCode, bd.Quantity
into v_bid,v_bdate,v_sdate,v_sid,v_bcode,v_ga
from bookingmaster bm, bookingdetails bd
where bm.sdate=new.SDate and bm.BID=bd.BID and bd.BCode='GAP'
and bm.BID=new.BID;
end;
WHILE (i<=v_ga) DO
Insert into temp_boo(BID,BDate,SDate,SID,BCode)
VALUES(new.BID,new.BDate,new.SDate,new.SID,new.BCode);
SET i=i+1;
END WHILE;
set i=1;
begin
select bm.BID,bm.BDate,bm.SDate,bm.SID,bd.BCode, bd.Quantity
into v_bid,v_bdate,v_sdate,v_sid,v_bcode,v_jj
from bookingmaster bm, bookingdetails bd
where bm.sdate=new.SDate and bm.BID=bd.BID and bd.BCode='JJP'
and bm.BID=new.BID;
end;
WHILE (i<=v_jj) DO
Insert into temp_boo(BID,BDate,SDate,SID,BCode)
VALUES(new.BID,new.BDate,new.SDate,new.SID,new.BCode);
SET i=i+1;
END WHILE;
set i=1;
begin
select bm.BID,bm.BDate,bm.SDate,bm.SID,bd.BCode, bd.Quantity
into v_bid,v_bdate,v_sdate,v_sid,v_bcode,v_ss
from bookingmaster bm, bookingdetails bd
where bm.sdate=new.SDate and bm.BID=bd.BID and bd.BCode='SSP'
and bm.BID=new.BID;
end;
WHILE (i<=v_ss) DO
START transaction;
Insert into temp_boo(BID,BDate,SDate,SID,BCode)
VALUES(new.BID,new.BDate,new.SDate,new.SID,new.BCode);
SET i=i+1;
END WHILE;
set i=1;
begin
select bm.BID,bm.BDate,bm.SDate,bm.SID,bd.BCode, bd.Quantity
into v_bid,v_bdate,v_sdate,v_sid,v_bcode,v_mj
from bookingmaster bm, bookingdetails bd
where bm.sdate=new.SDate and bm.BID=bd.BID and bd.BCode='MJP'
and bm.BID=new.BID;
end;
WHILE (i<=v_mj) DO
Insert into temp_boo(BID,BDate,SDate,SID,BCode)
VALUES(new.BID,new.BDate,new.SDate,new.SID,new.BCode);
SET i=i+1;
END WHILE;
Insert into bookinguser (BID,BDate,SDate,SID,BCode)
(select BID,BDate,SDate,SID,BCode from temp_boo
where (BID,BDate,SDate,SID,BCode)
not in (select BID,BDate,SDate,SID,BCode from bookinguser));
drop table temp_boo;
end
请让我知道我哪里做错了。
最佳答案
试试这个:
...
WHILE (i<=v_ss) DO
/*START transaction;*/
Insert into temp_boo(BID,BDate,SDate,SID,BCode)
VALUES(new.BID,new.BDate,new.SDate,new.SID,new.BCode);
SET i=i+1;
END WHILE;
...
更新
您不能在触发器内使用 DROP TABLE。请参阅13.3.3 Statements That Cause an Implicit Commit .
...
Insert into bookinguser (BID,BDate,SDate,SID,BCode)
(select BID,BDate,SDate,SID,BCode from temp_boo
where (BID,BDate,SDate,SID,BCode)
not in (select BID,BDate,SDate,SID,BCode from bookinguser));
/*drop table temp_boo;*/
...
更新1
更好的方法是:
13.3.3 Statements That Cause an Implicit Commit
"CREATE TABLE and DROP TABLE statements do not commit a transaction if the TEMPORARY keyword is used.".
...
WHILE (i<=v_ss) DO
/*START transaction;*/
Insert into temp_boo(BID,BDate,SDate,SID,BCode)
VALUES(new.BID,new.BDate,new.SDate,new.SID,new.BCode);
SET i=i+1;
END WHILE;
...
Insert into bookinguser (BID,BDate,SDate,SID,BCode)
(select BID,BDate,SDate,SID,BCode from temp_boo
where (BID,BDate,SDate,SID,BCode)
not in (select BID,BDate,SDate,SID,BCode from bookinguser));
drop temporary table temp_boo;
...
关于Mysql触发器显式隐式命令不允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22615186/