Mysql触发器显式隐式命令不允许

时间:2014-03-24 16:31:50

标签: mysql database triggers

我试图创建一个触发器,但它显示“不允许显式和隐式命令”。我试图在这里引用其他主题,但我不清楚。我的触发器代码。

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

请让我知道我在哪里做错了。

1 个答案:

答案 0 :(得分:0)

试试这个:

...
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;*/
...

SQL Fiddle demo

更新1

更好的方法是:

  

13.3.3 Statements That Cause an Implicit Commit

     

“如果,CREATE TABLE和DROP TABLE语句不提交事务   使用TEMPORARY关键字。“。

...
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;
...

SQL Fiddle demo