有2个表barcode和itbarcode,表barcode中有字段a_barcode,b_barcode我想做个触发器当我update表barcode时(修改a_barcode,b_barcode的值),触发事件 检测我刚输入的a_barcode,b_barcode的值是否在表itbarcode字段barcode里面,如果不存在就恢复原来的值,即不更新数据。
或者有其他方法,我该怎么做。谢谢

解决方案 »

  1.   

    在before UPDATE中
    set @ee=0;
    select count(*) into @ee from itbarcode where new.a_barcode=barcode or new.b_barcode=barcode;
    if @ee>=1 then
    set new.a_barcode=old.a_barcode;
    set new.b_barcode=old.b_barcode;
    end if
      

  2.   

    http://blog.csdn.net/ACMAIN_CHM/archive/2009/07/25/4380183.aspx
    MySQL 中如何在触发器里中断记录的插入或更新? 
      

  3.   

    create trigger test before update on barcode 
    for each row begin
    set @ee=0; 
    select count(*) into @ee from itbarcode where new.a_barcode=barcode or new.b_barcode=barcode; 
    if @ee>=1 then 
    set new.a_barcode=old.a_barcode; 
    set new.b_barcode=old.b_barcode; 
    end if
    end;
    错误提示:   执行错误 # 1064. 从数据库的响应:     You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 
    'end' at line 9怎么我贴的图 看不到呢  就我看的到?
      

  4.   

    我知道错在哪里啦  end if 后面少了一个冒号 晕 谢谢大家
      

  5.   

    还有个问题 就是 上面的 if 语句是反的 应该是if @ee=0 then