Mysql一步一步向导失败

时间:2014-03-22 14:20:28

标签: mysql stored-procedures wizard

我正在使用mysql触发器和存储过程创建一个向导。到目前为止我有这个

delimiter $$
CREATE TRIGGER le_trigger
AFTER INSERT ON inbox
FOR EACH ROW
BEGIN
declare last_inserted_number VARCHAR(100) DEFAULT '0800100200';
declare last_inserted_message VARCHAR(100) DEFAULT 'Lorem Ipsum';

set last_inserted_number = NEW.in_number;
set last_inserted_message = NEW.in_message;

if (not exists(select id from transactions where tel = last_inserted_number)) then
    insert into transactions(message, tel)
        values(last_inserted_message, last_inserted_number);
        insert into outbox(out_message, out_number)
        values("go to step 1", last_inserted_number);
else

   if ( exists(select id from transactions where tel = last_inserted_number && step_1='')) then
    update transactions set step_1=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 2", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_2='')) then
    update transactions set step_2=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 3", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_3='')) then
    update transactions set step_3=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 4", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_4='')) then
    update transactions set step_4=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 5", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_5='')) then
    update transactions set step_5=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 6", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_6='')) then
    update transactions set step_6=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 7", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_7='')) then
    update transactions set step_7=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 8", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_8='')) then
    update transactions set step_8=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 9", last_inserted_number);
end if;

   if ( exists(select id from transactions where tel = last_inserted_number && step_9='')) then
    update transactions set step_9=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("success.you have completed the form", last_inserted_number);
end if;

end if;

END$$
delimiter ;

但问题是所有ifs都是同时执行的,并没有实现类似行为的向导。计划是在插入触发器后观察表收件箱并获取消息和发件人号码。是否可以退出这是真的循环

else

   if ( exists(select id from transactions where tel = last_inserted_number && step_1='')) then
    update transactions set step_1=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 2", last_inserted_number);
end if;
exit..here

... more step_* code....

并继续在收件箱中插入?

1 个答案:

答案 0 :(得分:0)

不要将代码编写为单独的语句。使用elseif(您可以阅读文档here):

if (not exists(select id from transactions where tel = last_inserted_number)) then
    insert into transactions(message, tel)
        values(last_inserted_message, last_inserted_number);
        insert into outbox(out_message, out_number)
        values("go to step 1", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_1='')) then
    update transactions set step_1=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 2", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_2='')) then
    update transactions set step_2=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 3", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_3='')) then
    update transactions set step_3=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 4", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_4='')) then
    update transactions set step_4=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 5", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_5='')) then
    update transactions set step_5=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 6", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_6='')) then
    update transactions set step_6=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 7", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_7='')) then
    update transactions set step_7=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 8", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_8='')) then
    update transactions set step_8=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("go to step 9", last_inserted_number);
elseif ( exists(select id from transactions where tel = last_inserted_number && step_9='')) then
    update transactions set step_9=last_inserted_message where 
     tel=last_inserted_number;
       insert into outbox(out_message, out_number)
        values("success.you have completed the form", last_inserted_number);
end if;

我并非100%确定这是你想要的逻辑。如果没有,它应该指导你做什么。