表1
ID     名称          维修是否完成
1      洗衣机           0
2      电视机           0
表2
ID     维修最后完成工序   步骤1完成时间   步骤2完成时间     步骤3完成时间
1      步骤1             2005-1-1          null              null
2      步骤2             2005-2-2          2005-3-3          null在表2中触发
条件是: 如第二条"维修最后完成工序"指定的值为"步骤2",找到字段"步骤2"的完成时间字段,如果步骤2完成时间字段值不为空并且发生的变化,修改表1中ID=2的记录,将维修是否完成置1

解决方案 »

  1.   

    create trigger tr_表2_insert_update
    on 表2
    for insert,update
    as
    update a
    set 维修是否完成=1
    from 表1 a,inserted i
    where a.id=i.id
    and (
      i.维修最后完成工序='步骤1' and i.步骤1完成时间 is not null or
      i.维修最后完成工序='步骤2' and i.步骤2完成时间 is not null or
      i.维修最后完成工序='步骤3' and i.步骤3完成时间 is not null 
      )go
      

  2.   

    create trigger aa on table1
    for update
    as 
    if 维修最后完成工序='步骤2' and 步骤2完成时间 is not null
    begin
    update table1 set 维修是否完成='1' from table2 a,inserted where ID=a.ID
    end
      

  3.   

    create trigger t_update on table2
    for insert,update
    as
    begin
    if exists(select 1 from inserted where 维修最后完成工序 = '步骤2')
    update table1 set 维修是否完成 = 1
    from 
    (select a.* from inserted a left join table2 b on a.id = b.id and a.步骤2完成时间 <> b.步骤2完成时间)t1
    where table1.id = t1.idend
      

  4.   


     coolingpipe(冷箫轻笛) ( ) 信誉:100    Blog  2006-12-27 09:38:53  得分: 0  
     
     
       create trigger t_update on table2
    for insert,update
    as
    begin
    if exists(select 1 from inserted where 维修最后完成工序 = '步骤2')
    update table1 set 维修是否完成 = 1
    from 
    (select a.* from inserted a left join table2 b on a.id = b.id and a.步骤2完成时间 <> b.步骤2完成时间)t1
    where table1.id = t1.idend  
     
    --------------------------------------if exists(select 1 from inserted where 维修最后完成工序 = '步骤2')这样判断是否会出问题,inserted表中很可能有不止一个满足的,还有之前的记录。