表A
ID CODE 分配编号 分配数 实际编号 实际数
1 001 A01 20 NULL NULL
2 002 A01 30 NULL NULL表 B
ID 分配编号 实际编号 实际数
1 A01 A01 15
2 A01 B01 10
3 A01 B02 5
4 A01 B03 5实现表A为:
ID CODE 分配编号 分配数 实际编号 实际数
1 001 A01 20 A01 15
2 001 AO1 0 B01 53 002 A01 30 B01 5
4 002 A01 0 B02 5
5 002 A01 0 B03 5解释:
1.表A中 001 和 002 分别为2个单子
 001单子需要20件 002单子需要30件2.表B为汇总式的取东西的记录表
分配在A01上的东西 他分别在4个地方(A01,B01,B02,B03)取,实际数为分别取的数目3.首先是先满足001单子,因为001单子需要20件东西,但是在系统分配的地方A01中只取到15,不能满足20
所以就将在B01上取的10个,分5个给001,来满足001单子。
然后是满足002单子,将在B01拆分后剩余的5个分配给002单子,以此类推。希望高手帮忙

解决方案 »

  1.   

    ----------------------------------------------------------------
    -- Author  :SQL77(只为思齐老)
    -- Date    :2010-05-13 20:39:27
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86) 
    -- Oct 14 2005 00:33:37 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:#A
    if object_id('tempdb.dbo.#A') is not null drop table #A
    go 
    create table #A([ID] int,[CODE] varchar(3),[分配编号] varchar(3),[分配数] int,[实际编号] varchar(10),[实际数] int)
    insert #A
    select 1,'001','A01',20,null,null 
    union all
    select 2,'002','A01',30,null,null
    --> 测试数据:#B
    if object_id('tempdb.dbo.#B') is not null drop table #B
    go 
    create table #B([ID] int,[分配编号] varchar(3),[实际编号] varchar(3),[实际数] int)
    insert #B
    select 1,'A01','A01',15 union all
    select 2,'A01','B01',10 union all
    select 3,'A01','B02',5 union all
    select 4,'A01','B03',5
    --------------开始查询--------------------------
    if object_id('tempdb.dbo.#','u') is not null
    drop table #
    go
    select * into # from #a where 1<>1declare @分配编号 varchar(3),@code varchar(3),@分配数 int,
    @实际编号 varchar(3),@实际数 int,@id intset @分配编号='A01'declare cur_a cursor for 
              select code,[分配数] from #a where [分配编号]=@分配编号open cur_a
    fetch  next  from cur_a into @code,@分配数while @@fetch_status=0
    begin
         declare @status int 
     set @status=0
         declare cur_b cursor for 
    select id,实际编号,[实际数] from #b where  分配编号=@分配编号 and [实际数]<>0
         open cur_b
         fetch next from cur_b into @id,@实际编号 ,@实际数   while @@fetch_status=@status
         begin
              if @分配数>(select isnull(sum(实际数),0) 
    from # where CODE=@code and 分配编号=@分配编号)+@实际数
    begin
    update #b set 实际数=实际数-@实际数 where id=@id
                  insert #(id,CODE,[分配编号],[分配数] ,实际编号,实际数)
    select @id,@CODE,@分配编号,@分配数,@实际编号,@实际数
    --select * from #b
    end
               else
    begin
    update #b set 实际数=实际数-
                     ((select isnull(sum(实际数),0) from # where CODE=@code and 分配编号=@分配编号)+@实际数-@分配数)
    where id=@id              insert #(id,CODE,[分配编号],[分配数] ,实际编号,实际数)
    select @id,@CODE,@分配编号,@分配数,@实际编号,
     (select isnull(sum(实际数),0) from # where CODE=@code and 分配编号=@分配编号)+@实际数-@分配数
    --select * from #b
      set @status=1
    end
    fetch next from cur_b into @id,@实际编号 ,@实际数 

          end
    CLOSE cur_b
            DEALLOCATE cur_b
      fetch  next  from cur_a into @code,@分配数
    end
    CLOSE cur_a
    DEALLOCATE cur_aselect * from #
    --select * from #b
    ----------------结果----------------------------
    /* 
    ID          CODE 分配编号 分配数         实际编号       实际数
    ----------- ---- ---- ----------- ---------- -----------
    1           001  A01  20          A01        15
    2           001  A01  20          B01        5
    2           002  A01  30          B01        5
    3           002  A01  30          B02        5
    4           002  A01  30          B03        5(5 行受影响)
    */呵呵,练习了一下游标,哎,游标没怎么写过,整了好久,