在ps_djhz和ps_djmx兩表中建新增觸發器:
    ...
    update C
    set C.beizhu=case when A.danjbh=1 then 'aa' when A.danjbh=2 then 'bb'  end
    from ps_djhz A 
    inner join ps_djmx B on B.danjbh=A.danjbh  --兩表相同才更改
    inner join sp_kfpc C on C.dspid=B.dspid
     ...

解决方案 »

  1.   

    1.触发器只能建在一个表上,所以同时插入数据到 ps_djhz,ps_djmx 是不现实的.2.处理的方法当然是建视图,并在视图上建触发器,实现同时插入ps_djhz,ps_djmx 3.有几个疑问:
    a.触发器是在新增是才处理,这点确定吧? 
    b.ps_djhz,ps_djmx 表中,有主键吗?
    c.sp_kfpc 表中,原本就有记录吗? 
      

  2.   

    说明一下,在ps_djhz,ps_djmx新增时,改变sp_kfpc的beizhu 为ps_djhz的备注
    ps_djhz,ps_djmx的主键都为danjbh
    sp_kfpc里原本有记录,只不过我想修改备注字段为sp_djhz里的对应beizhu字段。回答邹大哥:
    如果我在ps_djmx 上加触发器,当它有insert 时,不就可以通过danjbh和dspid,picih,dkfid同时分别关联ps_djhz和sp_kfpc吗?
      

  3.   

    --处理的触发器
    create trigger tr_insert on ps_djmx
    for insert
    as
    update sp_kfpc set beizhu=c.beizhu
    from sp_kfpc a 
    join inserted b on a.dspid=b.dspid
    and a.picih=b.picih and a.dkfid=b.dkfid
    join ps_djhz c on b.danjbh=c.danjbh
    go
      

  4.   

    --测试--测试数据
    create table ps_djhz(danjbh int,beizhu varchar(10))
    insert ps_djhz select 1,'aa'
    union  all     select 2,'bb'create table ps_djmx(danjbh int,dspid char(3),picih int,dkfid char(3))create table sp_kfpc(dspid char(3),picih int,dkfid char(3),beizhu varchar(10))
    insert sp_kfpc(dspid,picih,dkfid)
              select '001',11,'001'
    union all select '001',22,'001'
    union all select '002',11,'001'
    union all select '003',11,'002'
    go--处理的触发器
    create trigger tr_insert on ps_djmx
    for insert
    as
    update sp_kfpc set beizhu=c.beizhu
    from sp_kfpc a 
    join inserted b on a.dspid=b.dspid
    and a.picih=b.picih and a.dkfid=b.dkfid
    join ps_djhz c on b.danjbh=c.danjbh
    go--插入记录
    insert ps_djmx select 1,'001',11,'001'
    union  all     select 1,'002',11,'001'
    union  all     select 2,'001',22,'001'
    union  all     select 2,'003',11,'002'
    go--显示处理的结果
    select * from ps_djmx
    select * from sp_kfpc
    go--删除测试
    drop table ps_djhz,ps_djmx,sp_kfpc/*--测试结果danjbh      dspid picih       dkfid 
    ----------- ----- ----------- ----- 
    1           001   11          001
    1           002   11          001
    2           001   22          001
    2           003   11          002(所影响的行数为 4 行)
    dspid picih       dkfid beizhu     
    ----- ----------- ----- ---------- 
    001   11          001   aa
    001   22          001   bb
    002   11          001   aa
    003   11          002   bb(所影响的行数为 4 行)--*/
      

  5.   

    多谢,让我先试一下,上次你回答的问题我还没有给你分呢
    不知道怎么给,TMD
      

  6.   

    我的怎么不行啊,我是danjbh为1的先插入,然后插2的
    sp_kfpc里的beizhu 就是不变啊
      

  7.   

    --不会吧,你看我的测试,你检查一下,是否有数据不匹配--测试--测试数据
    create table ps_djhz(danjbh int,beizhu varchar(10))
    insert ps_djhz select 1,'aa'
    union  all     select 2,'bb'create table ps_djmx(danjbh int,dspid char(3),picih int,dkfid char(3))create table sp_kfpc(dspid char(3),picih int,dkfid char(3),beizhu varchar(10))
    insert sp_kfpc(dspid,picih,dkfid)
              select '001',11,'001'
    union all select '001',22,'001'
    union all select '002',11,'001'
    union all select '003',11,'002'
    go--处理的触发器
    create trigger tr_insert on ps_djmx
    for insert
    as
    update sp_kfpc set beizhu=c.beizhu
    from sp_kfpc a 
    join inserted b on a.dspid=b.dspid
    and a.picih=b.picih and a.dkfid=b.dkfid
    join ps_djhz c on b.danjbh=c.danjbh
    go--逐条插入记录
    insert ps_djmx select 1,'001',11,'001'
    insert ps_djmx select 1,'002',11,'001'
    insert ps_djmx select 2,'001',22,'001'
    insert ps_djmx select 2,'003',11,'002'
    go--显示处理的结果
    select * from ps_djmx
    select * from sp_kfpc
    go--删除测试
    drop table ps_djhz,ps_djmx,sp_kfpc/*--测试结果danjbh      dspid picih       dkfid 
    ----------- ----- ----------- ----- 
    1           001   11          001
    1           002   11          001
    2           001   22          001
    2           003   11          002(所影响的行数为 4 行)
    dspid picih       dkfid beizhu     
    ----- ----------- ----- ---------- 
    001   11          001   aa
    001   22          001   bb
    002   11          001   aa
    003   11          002   bb(所影响的行数为 4 行)--*/
      

  8.   

    如果我先插入ps_djmx,再插ps_djhz,是不是就不能实现把ps_djhz里的beizhu 复制
    给sp_kfpc里的beizhu了?
      

  9.   

    应该是这样的,那我把触发器建在ps_djhz上可以吗?
      

  10.   

    --如果你是先插入 ps_djmx,那触发器应该建在 ps_djhz表上--触发器代码
    create trigger tr_insert on ps_djhz
    for insert
    as
    update sp_kfpc set beizhu=c.beizhu
    from sp_kfpc a 
    join ps_djmx b on a.dspid=b.dspid
    and a.picih=b.picih and a.dkfid=b.dkfid
    join inserted c on b.danjbh=c.danjbh
    go
      

  11.   

    呵呵,I get it!
    这次简单过头了吧,这么快就结了!