有三个不同类型的单据A,B,C
流程设定是根据单据A可以生成单据B,根据单据B可以生成单据C
1. 单据A描述-------------------------------------------------单据A有表头和表体如下,它们是通过billid关联
表头对应表a1定义如下
(billid:单据内部联络号,billno:单据编号)
billid   billno   
001      A001       
002      A002       表体对应表a2定义如下
(billid:单据内部联络号,flineed:表体行号,fmaterialid:物料编号,received:是否收到)
billid   flineid    fmaterialid   received
001       1           1180         未到
001       2           2936         未到
002       1           3690         未到
002       2           3689         未到2. 单据B描述-------------------------------------------------单据B有表头和表体如下,它们是通过billid关联
表头对应表B1定义如下
(billid:单据内部联络号,billno:单据编号)
billid   billno   
001      B001       
002      B002       表体对应表b2定义如下
(billid:单据内部联络号,flineed:表体行号,fmaterialid:物料编号,received:是否收到)
billid   flineid    fmaterialid     FSOURCEBILLNO
001       1           1180             A001
001       2           2936             A001
002       1           3690             A002
002       2           3689             A002说明:单据A和B的关系是:单据B的表体B2的字段FOURCEBILLNO对应单据A的表头A1的字段billno
1. 单据C描述------------------------------------------------
单据C有表头和表体如下,它们是通过billid关联
表头对应表C1定义如下
(billid:单据内部联络号,billno:单据编号)
billid   billno   
001      C001       
002      C002      表体对应表C2定义如下
(billid:单据内部联络号,flineed:表体行号,fmaterialid:物料编号,received:是否收到)
billid   flineid    fmaterialid   received
001       1           1180         已收
001       2           2936         已收
002       1           3690         未到
002       2           3689         未到要实现的功能是:
因为单据A还不知道是否收到货物,所以字段received显示未到,但是经过流程C的时候就可以确认是否到货了。
所以要求如果单据c的表体c2录入或更新成"已收"的时候,单据A的表体A2字段received跟新成"已收"
麻烦各位大哥给我写一个触发器。客户比较急。

解决方案 »

  1.   

    主要是中间多了一个单据B,它连接单据A和C.要求单据C更新的时候直接去更新A。单据B只是过渡。
      

  2.   

    create trigger tr_update
    on C2
    for update
    as
    begin
        update ta
        set received = '已收'
        from inserted i
        left join a2 ta  on ta .billid  = i.billid  and i.fmaterialid  = ta.fmaterialid
        where i.received = '已收'
    end
    go
     
        
      

  3.   

    要求插入和更新表C2的时候都要更新A2。楼上回答有问题,表A2和C2没有直接联系。他们是通过B1和B2来建立关系的。
      

  4.   


    只需要C2和A2连接就可以了,通过bill_id和fmaterialid  就行了。
      

  5.   

    各位大哥,billid只是对表头和表体关联,A2,B2,C2的billid没有联系。
      

  6.   

    说明:单据A和B的关系是:单据B的表体B2的字段FOURCEBILLNO对应单据A的表头A1的字段billno
    说明:单据B和C的关系是:单据C的表体C2的字段FOURCEBILLNO对应单据B的表头B1的字段billno
    A,B,C三张表的关系就是依靠fsourcebillno(源单)来连接的。
      

  7.   

    C2的字段FOURCEBILLNO
    你找我看看在哪儿?
      

  8.   


    create trigger tr_update 
    on C2 
    for update 
    as 
    begin 
        update ta 
        set received = '已收' 
        from inserted i 
        left join b1 on b1.billno   = ta.FOURCEBILLNO and i.fmaterialid  = b1.fmaterialid 
        join (select distinct billid,FSOURCEBILLNO from b2) b on b.billid  = b1.billid 
        join a1 on a1.billno  = b.FSOURCEBILLNO
        join a2 ta  on ta .billid  = i.billid
        where i.received = '已收' 
    end 
    go 
      

  9.   


    if(OBJECT_ID('A1') is not null)
      drop table A1
    create table A1(billid char(3),  billno varchar(10))
    go
    insert into A1
      select '001', 'A001'     
      union all select '002',  'A002'
      
    goif(OBJECT_ID('A2') is not null)
      drop table A2
    create table A2(billid char(3), flineid int, fmaterialid int, received varchar(20)) go insert into A2
      select '001',      1 ,         1180  ,      '未到'
      union all select '001',      2 ,         2936 ,       '未到'
      union all select '002',      1 ,         3690  ,      '未到'
      union all select '002',      2  ,        3689 ,       '未到'
      
      go
      
     if(OBJECT_ID('B1') is not null)
      drop table B1
    create table B1(billid char(3),  billno varchar(10))
    go
    insert into B1
      select '001', 'B001'     
      union all select '002',  'B002'
      
    goif(OBJECT_ID('B2') is not null)
      drop table B2
    create table B2(billid char(3), flineid int, fmaterialid int, 
        FSOURCEBILLNO varchar(10)) go insert into B2
      select '001',      1 ,         1180  ,      'A001'
      union all select '001',      2 ,         2936 ,       'A001'
      union all select '002',      1 ,         3690  ,      'A002'
      union all select '002',      2  ,        3689 ,       'A002'   
      
      go
      
    if(OBJECT_ID('C1') is not null)
      drop table C1
    create table C1(billid char(3),  billno varchar(10))
    go
    insert into C1
      select '001', 'C001'     
      union all select '002',  'C002'
      
    goif(OBJECT_ID('C2') is not null)
      drop table C2
    create table C2(billid char(3), flineid int, fmaterialid int, 
        FSOURCEBILLNO varchar(10),received varchar(20)) go insert into C2
      select '001',      1 ,         1180  ,      'B001','未到'
      union all select '001',      2 ,         2936 ,       'B001','未到'
      union all select '002',      1 ,         3690  ,      'B002','未到'
      union all select '002',      2  ,        3689 ,       'B002' , '未到' 
      
      go
      
    create trigger tr_update
    on C2
    for update,insert
    as
    begin
        update a2 
        set received = '已收' 
        from inserted i 
        join (select b1.billno, b2.fmaterialid,b2.FSOURCEBILLNO from b2 join b1 on b2.billid=b1.billid) b
          on i.FSOURCEBILLNO=b.billno and b.fmaterialid=i.fmaterialid
         join a1 on b.FSOURCEBILLNO=a1.billno
         join a2 on a1.billid=a2.billid and a2.fmaterialid=i.fmaterialid
          
        where i.received = '已收'
    end
    go update C2 set received='已收' where billid='001'select * from A2
    billid flineid fmaterialid received
    001 1 1180 已收
    001 2 2936 已收
    002 1 3690 未到
    002 2 3689 未到
      

  10.   

    关键是update语句
    你可以先把select语句写出来,然后再把前面的换成update相应的表
    这是思路。
    一般人我不告诉他...
      

  11.   

    /*
    Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)   Jul  9 2008 14:43:34   Copyright (c) 
    1988-2008 Microsoft Corporation  Enterprise Evaluation Edition on Windows NT 5.1 <X86> 
    (Build 2600: Service Pack 3) 
     愿和大家共同进步
    如有雷同、实属巧合
    ●●●●●2009-09-17 00:55:00.327●●●●●
     ★★★★★soft_wsx★★★★★
    */
    if object_id('wzY_syllabus') is not null drop table  wzY_syllabus
      go
    create table wzY_syllabus ( 
      Id                  int   ,                --标识Id 
      Number              varchar(20)          not null, --课程编号 
      Name                varchar(28)          null, --课程名称 
      Period              int                  null, --周学时 
      Kind                varchar(16)          null, --课程性质,考试/考查 
      Type                varchar(16)          null, --课程类型,公共基础/专业基础/专业课/公共选修/专业选修 
      CreditHour          smallint            null, --学分 
      constraint PK_WZY_SYLLABUS primary key (Number) 

    go
    insert into wzY_syllabus values(1,101,'语文',10,'考试','专业基础',150)
    insert into wzY_syllabus values(2,102,'数据',10,'考试','专业基础',150)
    insert into wzY_syllabus values(3,103,'英语',10,'考试','专业基础',150)
    insert into wzY_syllabus values(4,104,'政治',10,'考试','专业基础',100)/*==============================================================*/ 
    /* Table: wzY_Grade  成绩表                                    */ 
    /*==============================================================*/ 
    if object_id('wzY_Grade') is not null drop table  wzY_Grade
      go
    create table wzY_Grade ( 
      Id                  int                  identity, --标识Id 
      StuNumber            varchar(20)          not null, --外键,学号 
      SylNumber            varchar(20)          not null, --外键,课程编号 
      Mark                int                  null, --分数 
      constraint PK_WZY_GRADE primary key (StuNumber, SylNumber) --联合主键(学号,课程编号) 

    go
    --成绩表上的触发器
    create  trigger dbo.tr_wzY_Grade on dbo.wzY_Grade
    for insert
    as
    begin
      if not exists(select b.* from inserted a                        --判断是否是第一次维护总分表,是则插入
                            join wzY_CreditHour b on a.StuNumber=b.StuNumber 
                    )
         insert into dbo.wzY_CreditHour(StuNumber,CreditHour)
             select a.StuNumber,a. from dbo.wzY_Grade a                       
                            join inserted b on a.StuNumber=b.StuNumber
      else 
        update a set a.CreditHour=isnull(a.CreditHour,0)+isnull(d.,0)                    --更新
          from wzY_CreditHour a 
             join inserted b on a.StuNumber=b.StuNumber
             join wzY_Grade  d  on b.stunumber=d.stunumber and b.SylNumber=d.SylNumber
    end
    go/*==============================================================*/ 
    /* Table: wzY_CreditHour  总学分分表                            */ 
    /*==============================================================*/ 
    if object_id('wzY_CreditHour') is not null drop table  wzY_CreditHour
      go
    create table wzY_CreditHour ( 
      Id                  int                  identity, --标识Id 
      StuNumber            varchar(20)          not null, --外键,主键,学号 
      CreditHour          int                  null default 0, --总学分 
      constraint PK_WZY_CREDITHOUR primary key (StuNumber) 

    go--测试数据
    insert wzY_Grade(StuNumber,SylNumber,Mark) values(1,101,123)
    insert wzY_Grade(StuNumber,SylNumber,Mark) values(1,102,120)
    insert wzY_Grade(StuNumber,SylNumber,Mark) values(1,103,90)
    insert wzY_Grade(StuNumber,SylNumber,Mark) values(1,104,129)insert wzY_Grade(StuNumber,SylNumber,Mark) values(2,101,90)
    insert wzY_Grade(StuNumber,SylNumber,Mark) values(2,102,90)
    insert wzY_Grade(StuNumber,SylNumber,Mark) values(2,103,90)
    insert wzY_Grade(StuNumber,SylNumber,Mark) values(2,104,31)insert wzY_Grade(StuNumber,SylNumber,Mark) values(3,101,101)
    insert wzY_Grade(StuNumber,SylNumber,Mark) values(3,102,101)
    insert wzY_Grade(StuNumber,SylNumber,Mark) values(3,103,120)
    insert wzY_Grade(StuNumber,SylNumber,Mark) values(3,104,120)select * from wzY_Grade
    /*
    Id StuNumber SylNumber Mark
    1 1 101 123
    2 1 102 120
    3 1 103 90
    4 1 104 129
    5 2 101 90
    6 2 102 90
    7 2 103 90
    8 2 104 31
    9 3 101 101
    10 3 102 101
    11 3 103 120
    12 3 104 120
    */
    select * from wzY_CreditHour
    /*
    Id StuNumber CreditHour
    1 1 462
    2 2 301
    3 3 442
    */