如果我更新a表的f1,f3,f4字段
-----
3个都改才算,还是只要其中某一个更改就行?

解决方案 »

  1.   

    如果我更新a表的f1,f3,f4字段是更改其中任意一个
      

  2.   

    3、如果更新a表中的f5,假设由0改为1,则删除b表中相对应的记录(即b.id=a.id)
    ---------------
    这步:如果同时更新f1,f3,f4(之一或组合)字段,怎么处理
      

  3.   

    LZ 的问题太多了,看的我头晕我只想说这个触发器中要用好 UPDATE() 函数,问题就好解决了另外以我的能力是无法解决的
      

  4.   

    /*
    需要两个触发器:一个DELETE,一个UPDATE
    */
    --UPDATE触发器
    create trigger a_update
    on A表
    for update
    as
    set nocount onif update(f5)
    begin
    --2、如果更新a表中的f5,假设由1改为0,不管是否更改了f1,f3,f4等其它字段,此时检索b表中是否具有相同的记录(即a.id=b.id),若有,则更新b表中的数据为a表更新前的数据(注意一定是更新前的);若没有,则将a表中更新前的记录(注意一定是更新前的)插入到b表中
    update a set a.f1=b.f1,a.f2=b.f2,a.f3=b.f3,a.f4=b.f4,a.f5=b.f5 from B表 a join deleted b on a.id=b.id join inserted c on a.id=c.id where b.f5=1 and c.f5=0
    --3、如果更新a表中的f5,假设由0改为1,则删除b表中相对应的记录(即b.id=a.id)
    delete a from B表 a join deleted b on a.id=b.id join inserted c on a.id=c.id where b.f5=0 and c.f5=1
    /*这里不知道是否可以返回,因为我不知道,我上面问了你没答。*/
    --return
    end--1、如果我更新a表的f1,f3,f4字段,此时检索b表是否具有相同的记录(即a.id=b.id),若有,则更新b表的数据为a表更新前的数据(注意一定是更新前的);若没有,则什么都不做
    if (columns_updated() & 26) > 0
    /*关于26,由下公式计算,colid是指syscolumns表中的colid:power(2,f1.colid-1)+power(2,f3.colid-1)+power(2,f4.colid-1)=26*/
    begin
    update a set a.f1=b.f1,a.f2=b.f2,a.f3=b.f3,a.f4=b.f4,a.f5=b.f5 from B表 a join deleted b on a.id = b.id
    endset nocount off
    go
    --DELETE触发器
    create trigger a_delete
    on A表
    for delete
    as
    set nocount on
    --4、如果删除a表中的记录,应同时删除b表中相同的记录
    delete B表 from deleted where B表.id=deleted.id
    set nocount off
    go
      

  5.   

    /*
    针对UPDATE触发器做了一些优化
    */--UPDATE触发器
    create trigger a_update
    on A表
    for update
    as
    set nocount onif update(f5)
    begin
    -- 2、如果更新a表中的f5,假设由1改为0,不管是否更改了f1,f3,f4等其它字段,此时检索b表中是否具有相同的记录(即a.id=b.id)
    if exists (select 1 from deleted b join inserted c on b.id=c.id and b.f5=1 and c.f5=0)
    begin
    --若有,则更新b表中的数据为a表更新前的数据(注意一定是更新前的);
    update a set a.f1=b.f1,a.f2=b.f2,a.f3=b.f3,a.f4=b.f4,a.f5=b.f5 --更新什么字段,自己决定。
    from B表 a
    join deleted b
    on a.id=b.id
    join inserted c
    on a.id=c.id
    where b.f5=1 and c.f5=0
    --若没有,则将a表中更新前的记录(注意一定是更新前的)插入到b表中
    insert B表
    select b.*
    from deleted b
    join inserted c
    on b.id=c.id
    where not exists (select 1 from B表 where id=b.id)
    --这个动作会比较耗时——上面少写了这一步
    end
    --3、如果更新a表中的f5,假设由0改为1
    if exists (select 1 from deleted b join inserted c on b.id=c.id and b.f5=0 and c.f5=1)
    begin
    --则删除b表中相对应的记录(即b.id=a.id)
    delete a from B表 a join deleted b on a.id=b.id join inserted c on a.id=c.id where b.f5=0 and c.f5=1
    end
    --这里不知道是否可以返回,因为我不知道,我上面问了你没答。
    --return
    end
    -------------------------------------------------
    --1、如果我更新a表的f1,f3,f4字段,此时检索b表是否具有相同的记录(即a.id=b.id)
    if (columns_updated() & 26) > 0
    /*关于26,由下公式计算,colid是指syscolumns表中的colid:power(2,f1.colid-1)+power(2,f3.colid-1)+power(2,f4.colid-1)=26*/
    begin
    --若有,则更新b表的数据为a表更新前的数据(注意一定是更新前的);若没有,则什么都不做
    update a set a.f1=b.f1,a.f2=b.f2,a.f3=b.f3,a.f4=b.f4,a.f5=b.f5 from B表 a join deleted b on a.id = b.id
    endset nocount off
    go
      

  6.   

    /*
    针对UPDATE触发器做了一些优化——最终版
    */--UPDATE触发器
    create trigger a_update
    on A表
    for update
    as
    set nocount onif update(f5)
    begin
    -- 2、如果更新a表中的f5,假设由1改为0,不管是否更改了f1,f3,f4等其它字段,此时检索b表中是否具有相同的记录(即a.id=b.id)
    if exists (select 1 from deleted b join inserted c on b.id=c.id where b.f5=1 and c.f5=0)
    begin
    --若有,则更新b表中的数据为a表更新前的数据(注意一定是更新前的);
    update a set a.f1=b.f1,a.f2=b.f2,a.f3=b.f3,a.f4=b.f4,a.f5=b.f5 --更新什么字段,自己决定。
    from B表 a
    join deleted b
    on a.id=b.id
    join inserted c
    on a.id=c.id
    where b.f5=1 and c.f5=0
    --若没有,则将a表中更新前的记录(注意一定是更新前的)插入到b表中
    insert B表
    select b.*
    from deleted b
    join inserted c
    on b.id=c.id
    where b.f5=1 and c.f5=0 and not exists (select 1 from B表 where id=b.id)
    --这个动作会比较耗时——上面少写了这一步
    end
    --3、如果更新a表中的f5,假设由0改为1
    if exists (select 1 from deleted b join inserted c on b.id=c.id where b.f5=0 and c.f5=1)
    begin
    --则删除b表中相对应的记录(即b.id=a.id)
    delete a from B表 a join deleted b on a.id=b.id join inserted c on a.id=c.id where b.f5=0 and c.f5=1
    end
    --这里不知道是否可以返回,因为我不知道,我上面问了你没答。
    --return
    end--1、如果我更新a表的f1,f3,f4字段,此时检索b表是否具有相同的记录(即a.id=b.id)
    if (columns_updated() & 26) > 0
    /*关于26,由下公式计算,colid是指syscolumns表中的colid:power(2,f1.colid-1)+power(2,f3.colid-1)+power(2,f4.colid-1)=26*/
    begin
    --若有,则更新b表的数据为a表更新前的数据(注意一定是更新前的);若没有,则什么都不做
    update a set a.f1=b.f1,a.f2=b.f2,a.f3=b.f3,a.f4=b.f4,a.f5=b.f5 from B表 a join deleted b on a.id = b.id
    endset nocount off
    go
      

  7.   

    TO Limpire(昨夜小楼):
    --若有,则更新b表中的数据为a表更新前的数据(注意一定是更新前的);
    update a set a.f1=b.f1,a.f2=b.f2,a.f3=b.f3,a.f4=b.f4,a.f5=b.f5 --我要的是更新B表,你这应该更新a表吧?。
    from B表 a  --什么意思看不懂
    join deleted b
    on a.id=b.id
    join inserted c
    on a.id=c.id
    where b.f5=1 and c.f5=0主对上以上我的注释给解释一下,谢谢了。
      

  8.   

    from B表 a --什么意思看不懂
    --------------------
    a 是 B表的别名update a set .... from B表 a ...
    就是更新B表
      

  9.   

    --不放心就不用别名,用全名吧:
    update B表 set B表.f1=deleted.f1,B表.f2=deleted.f2,B表.f3=deleted.f3,B表.f4=deleted.f4,B表.f5=deleted.f5
    from B表
    join deleted
    on B表.id=deleted.id
    join inserted
    on B表.id=inserted.id
    where deleted.f5=1 and inserted.f5=0