CC 表里有的 DD 表中一定有吗?若是往CC里插入一条新纪录,DD中是否也应插入呢?两个表的纪录是不是同时插入的?
试试这句话,当你往cc里插入完时,执行下面这句话。
UPDATE DD SET dquantity=sum(cquantity) from cc,DD where CC.pid='tom' AND DD.pid=CC.pid

解决方案 »

  1.   

    用"触发器"找以前的贴,能找到很多!另外,你的逻辑不清楚,难道你要记录最后一条插入到CC的数据到DD?
    删除、修改CC表的时候怎么办?
      

  2.   

    TO HAIWER ,vfp_database:
    dd表里的dquantity是对应的相同pid的表cc里的cquantity之和
    所以不管插入,修改,还是删除,都会影响dquantity的值。
    用程序代码实现很麻烦,所以我才想用触发器实现。
    TO echo_llee:
    UPDATE DD SET dquantity=sum(cquantity) from cc,DD where CC.pid='tom' AND DD.pid=CC.pid 
    里 CC.pid='tom'如果把tom值换成变量,该如何写?另:我用的是MSSQLSERVER2000。
    多谢!
      

  3.   

    create TRIGGER tr_CC
    on CC
    for insert,delete,update
    asupdate dd set dquantity=t.rs
    from dd,(
    select pid,sum(cquantity) as rs from cc where pid in (select pid from inserted union select pid from deleted) 
    group by pid
    ) t
    where t.pid=dd.pidgo
    2 update dd set dquantity=rs where pid='tom'
      

  4.   

    sorry,多了个尾巴!create TRIGGER tr_CC
    on CC
    for insert,delete,update
    asupdate dd set dquantity=t.rs
    from dd,(
    select pid,sum(cquantity) as rs from cc where pid in (select pid from inserted union select pid from deleted) 
    group by pid
    ) t
    where t.pid=dd.pidgo