Table1 EntreeID  EntreeQty OutedQty  EntreeDate
001       100       0         2007-1-1
002       50        0         2007-1-2
003       50        0         2007-1-3Table2
OutID     OutQty  OutDate
当插入数据时
001       160     2007-1-3
时,更新Table 为
EntreeID  EntreeQty OutedQty  EntreeDate
001       100       100       2007-1-1
002       50        50        2007-1-2
003       50        10        2007-1-3
如何实现这种果,,

解决方案 »

  1.   

    你要的是触发器吧?CREATE TRIGGER [TRIGGER01] ON Table2FOR INSERT
    AS
    update Table1  set OutedQty=EntreeQty
      

  2.   

    是EntreeID  EntreeQty OutedQty  EntreeDate
    001       100       100       2007-1-1
    002       50        50        2007-1-2
    003       50        10        2007-1-3
    ????
    还是
    EntreeID  EntreeQty OutedQty  EntreeDate
    001       100       100       2007-1-1
    002       50        50        2007-1-2
    003       50        50        2007-1-3
    ????
      

  3.   

    如果是
    EntreeID  EntreeQty OutedQty  EntreeDate
    001       100       100       2007-1-1
    002       50        50        2007-1-2
    003       50        50        2007-1-3
    的话,上面那个触发器就可以
      

  4.   

    是EntreeID  EntreeQty OutedQty  EntreeDate
    001       100       100       2007-1-1
    002       50        50        2007-1-2
    003       50        10        2007-1-3
    sum(OutedQty)=160  ,也就是和输入的出库数一样
      

  5.   

    create table ta(EntreeID varchar(20), EntreeQty int, OutedQty int, EntreeDate datetime)
    insert ta select '001',       100,       0,         '2007-1-1'
    union all select '002',       50,        0,         '2007-1-2'
    union all select '003',       50,        0,         '2007-1-3'
    create  table tb(OutID varchar(20),    OutQty int, OutDate datetime)
    create trigger test_tr on tb
    for insert
    as
    update a
    set OutedQty=case when (select sum(EntreeQty)from ta where EntreeDate!>a.EntreeDate)<OutQty
             then EntreeQty else OutQty-
             ((select sum(EntreeQty)from ta where EntreeDate!>a.EntreeDate)-EntreeQty)end
    from ta a ,tb测试:
    insert tb select '001',       160,     '2007-1-3'select * from ta--drop table ta,tbEntreeID             EntreeQty   OutedQty    EntreeDate                                             
    -------------------- ----------- ----------- ------------------------------------------------------ 
    001                  100         100         2007-01-01 00:00:00.000
    002                  50          50          2007-01-02 00:00:00.000
    003                  50          10          2007-01-03 00:00:00.000(所影响的行数为 3 行)
      

  6.   

    以上为触发器以下为存储过程实现
    create table ta(EntreeID varchar(20), EntreeQty int, OutedQty int, EntreeDate datetime)
    insert ta select '001',       100,       0,         '2007-1-1'
    union all select '002',       50,        0,         '2007-1-2'
    union all select '003',       50,        0,         '2007-1-3'
    create  table tb(OutID varchar(20),    OutQty int, OutDate datetime)--用存储过程
    create proc test_p 
    @OutID varchar(20),  
    @OutQty int,
    @OutDate datetime
    as
    insert tb select @OutID,@OutQty,@OutDate
    update a
    set OutedQty=case when (select sum(EntreeQty)from ta where EntreeID!>a.EntreeID)<OutQty--EntreeDate可以换为EntreeID(如果EntreeID是递增列的情况下)
             then EntreeQty else OutQty-
             ((select sum(EntreeQty)from ta where EntreeID!>a.EntreeID)-EntreeQty)end
    from ta a ,tb测试:
    exec test_p '001',   160,     '2007-1-3'select * from ta--drop table ta,tb
    EntreeID             EntreeQty   OutedQty    EntreeDate                                             
    -------------------- ----------- ----------- ------------------------------------------------------ 
    001                  100         100         2007-01-01 00:00:00.000
    002                  50          50          2007-01-02 00:00:00.000
    003                  50          10          2007-01-03 00:00:00.000(所影响的行数为 3 行)
      

  7.   

    有问题测试前再加几条记录
    insert ta select '004',       50,       0,         '2007-1-4'
    union all select '005',       50,        0,         '2007-1-5'
    union all select '006',       50,        0,         '2007-1-6'然后insert tb select '001',       160,     '2007-1-3'试试就不行了
      

  8.   

    高手多多,满意多多。
    谢谢 roy_88(中国风_燃烧你的激情!!!) 
    也谢谢各位的参与。
      

  9.   

    楼主的更新时可以在Table1 新增一下出库标识作为判断
      

  10.   

    TO roy_88(中国风_燃烧你的激情!!!) 你写的这种,当OutQty为0的时候可以用,但是如果再在这个基础上录入数据所以感觉用游标更好一点
      

  11.   

    加条件实现:
    create table ta(EntreeID varchar(20), EntreeQty int, OutedQty int, EntreeDate datetime)
    insert ta select '001',       100,       100,         '2007-1-1'
    union all select '002',       50,        50,         '2007-1-2'
    union all select '003',       50,        10,         '2007-1-3'
    union all select '004',       50,        0,         '2007-1-4'
    union all select '005',       50,        0,         '2007-1-5'
    union all select '006',       50,        0,         '2007-1-6'
    create  table tb(OutID varchar(20),    OutQty int, OutDate datetime)
    insert tb select '001',   160,     '2007-1-3'
    --用存储过程
    create proc test_p 
    @OutID varchar(20),  
    @OutQty int,
    @OutDate datetime
    as
    insert tb select @OutID,@OutQty,@OutDate
    update a
    set OutedQty=case when (select sum(EntreeQty-OutedQty)from ta where EntreeID!>a.EntreeID)<OutQty--EntreeDate可以换为EntreeID(如果EntreeID是递增列的情况下)
     then EntreeQty else OutQty-
             ((select sum(EntreeQty-OutedQty) from ta where EntreeID!>a.EntreeID)-EntreeQty)end
    from ta a ,tb
    where OutedQty<EntreeQty and tb.OutID=@OutID测试:
    exec test_p '001',   160,     '2007-1-3'select * from ta--drop table ta,tb
    --drop proc test_p
    EntreeID             EntreeQty   OutedQty    EntreeDate                                             
    -------------------- ----------- ----------- ------------------------------------------------------ 
    001                  100         100         2007-01-01 00:00:00.000
    002                  50          50          2007-01-02 00:00:00.000
    003                  50          50          2007-01-03 00:00:00.000
    004                  50          50          2007-01-04 00:00:00.000
    005                  50          50          2007-01-05 00:00:00.000
    006                  50          20          2007-01-06 00:00:00.000(所影响的行数为 6 行)
      

  12.   

    加条件实现:
    create table ta(EntreeID varchar(20), EntreeQty int, OutedQty int, EntreeDate datetime)
    insert ta select '001',       100,       100,         '2007-1-1'
    union all select '002',       50,        50,         '2007-1-2'
    union all select '003',       50,        10,         '2007-1-3'
    union all select '004',       50,        0,         '2007-1-4'
    union all select '005',       50,        0,         '2007-1-5'
    union all select '006',       50,        0,         '2007-1-6'
    create  table tb(OutID varchar(20),    OutQty int, OutDate datetime)
    insert tb select '001',   160,     '2007-1-3'
    --用存储过程
    create proc test_p 
    @OutID varchar(20),  
    @OutQty int,
    @OutDate datetime
    as
    insert tb select @OutID,@OutQty,@OutDate
    update a
    set OutedQty=case when (select sum(EntreeQty-OutedQty)from ta where EntreeID!>a.EntreeID)<OutQty--EntreeDate可以换为EntreeID(如果EntreeID是递增列的情况下)
     then EntreeQty else OutQty-
             ((select sum(EntreeQty-OutedQty) from ta where EntreeID!>a.EntreeID)-EntreeQty)end
    from ta a ,tb
    where OutedQty<EntreeQty and tb.OutID=@OutID测试:
    exec test_p '002',   160,     '2007-1-3'--这里把插入编号改为002select * from ta
      

  13.   

    create table ta(EntreeID varchar(20), EntreeQty int, OutedQty int, EntreeDate datetime)
    insert ta select '001',       100,       0,         '2007-1-1'
    union all select '002',       50,        0,         '2007-1-2'
    union all select '003',       50,        0,         '2007-1-3'
    go
    create  table tb(OutID varchar(20),OutQty int,OutDate datetime)
    gocreate proc Sotreage_proc
    @OutID varchar(20),
    @OutQty int, 
    @OutDate datetime
    as
    declare cur cursor for select EntreeId,EntreeQty from ta where EntreeDate<=@OutDate order by EntreeDate
    declare @EntreeId int,@EntreeQty int
    declare @temp int
    set @temp=@OutQty
    open cur 
    fetch next from cur into @EntreeId,@EntreeQty
    while @@fetch_Status=0
    begin
    update ta set OutedQty=case when @EntreeQty>@temp then @temp else @EntreeQty end where EntreeId=@EntreeId
    set @temp=case when @EntreeQty<@temp then @temp-@EntreeQty else 0 end
            fetch next from cur into @EntreeId,@EntreeQty
    end
    close cur
    deallocate cur
    goexec Sotreage_proc '001','160','2007-1-3'
      

  14.   

    如下情况,只更新前三条记录,需要添加一个条件。
    create table ta(EntreeID varchar(20), EntreeQty int, OutedQty int, EntreeDate datetime)
    insert ta select '001',       100,       0,         '2007-1-1'
    union all select '002',       50,        0,         '2007-1-2'
    union all select '003',       50,        0,         '2007-1-3'
    union all select '004',       50,        0,         '2007-1-4'create  table tb(OutID varchar(20),    OutQty int, OutDate datetime)
    insert tb select '001',   160,     '2007-1-3'
    --用存储过程
    create proc test_p 
    @OutID varchar(20),  
    @OutQty int,
    @OutDate datetime
    as
    insert tb select @OutID,@OutQty,@OutDate
    update a
    set OutedQty=case when (select sum(EntreeQty-OutedQty)from ta where EntreeID!>a.EntreeID)<OutQty--EntreeDate可以换为EntreeID(如果EntreeID是递增列的情况下)
     then EntreeQty else OutQty-
             ((select sum(EntreeQty-OutedQty) from ta where EntreeID!>a.EntreeID)-EntreeQty)end
    from ta a ,tb
    where OutedQty<EntreeQty and tb.OutID=@OutID
    and
    isnull((select sum(EntreeQty-OutedQty)from ta where EntreeID<a.EntreeID and OutedQty<EntreeQty ),0)-OutedQty<OutQty--加多一个条件
    或直接用变量:
    create  table tb(OutID varchar(20),    OutQty int, OutDate datetime)
    insert tb select '001',   160,     '2007-1-3'
    --用存储过程
    create proc test_p 
    @OutID varchar(20),  
    @OutQty int,
    @OutDate datetime
    as
    insert tb select @OutID,@OutQty,@OutDate
    update a
    set OutedQty=case when (select sum(EntreeQty-OutedQty)from ta where EntreeID!>a.EntreeID)<@OutQty--EntreeDate可以换为EntreeID(如果EntreeID是递增列的情况下)
     then EntreeQty else @OutQty-
             ((select sum(EntreeQty-OutedQty) from ta where EntreeID!>a.EntreeID)-EntreeQty)end
    from ta a 
    where OutedQty<EntreeQty 
    and
    isnull((select sum(EntreeQty-OutedQty)from ta where EntreeID<a.EntreeID and OutedQty<EntreeQty ),0)-OutedQty<@OutQty
    测试:
    exec test_p '002',   160,     '2007-1-3'--这里把插入编号改为002