2016-06-07 38 views
0

我有一个表'chat_record',它有一列'chat_id'。对于每个'chat_id'都存在同名的表。现在,我想创建一个触发器,以便在删除chat_record中的条目时删除表。 这里是我的代码 - **触发器内的'DROP'表 - MySQL

create trigger drop_table before delete on chat_record 
for each row 
begin 
declare chatid varchar(20); 
set @chatid=(select chat_id from chat_record where chat_id=old.chat_id); 
drop table chatid; 
end; 

** 错误1422:显式或隐式提交不允许存储函数或触发器。

+1

请不要这样做。想象一下任何试图维护代码的人都会感到困惑。哦,桌子有时会消失 - 我不知道为什么?我可以建议你解释一下你有什么要求来引导你作为答案吗?即你不想创造一个灵活的解决问题的方法 - 你想通过易于理解和测试的方法解决问题?你试过用触发器 - 它不如你想要的那么可靠? –

回答

0
create or replace trigger drop_table1 before delete on chat_record 
for each row 
declare 
chatid1 varchar(20); 
pragma autonomous_transaction; 
begin 
select 1 into chatid1 from chat_record where chat_id=:old.chat_id; 
if chatid1 = 1 
then 
    EXECUTE IMMEDIATE 'drop table chatid'; 
end if; 
end;