如何遍历表并更新多行

时间:2018-07-27 03:20:49

标签: sql sql-server loops

您好,在下面的表格中,我有2个表格,其中名称是销售记录历史记录,另一个是销售信息。.基于销售信息表,乔什卖出了100个我如何更新销售记录历史记录表

场景:

josh软件包已经售出50个,如何将其添加到100个销售信息中 包装1 50添加到已售出,因为最大上限为100,售出的余额添加到josh的包装2中

table - sell Record history		
name	package	max cap	sold
Josh	1	100	0
Jack	1	100	0
Josh	2	100	0
Austin	1	100	0

table -sell info	
name	sold
Josh	150
Jack	0
Austin	0

预期输出将是:

name    |package|max cap    |sold
Josh    |1      |100        |100
Jack    |1      |100        |0
Josh    |2      |100        |50
Austin  |1      |100        |0

1 个答案:

答案 0 :(得分:2)

在一个简单的查询中无法做到这一点。我下面的查询使用递归CTE。

假设:数据包是数字,从1开始并以1为增量无间隙

-- Create sample table
declare @sell_record table
(
    name        varchar(6),
    package     int,
    max_cap     int,
    sold        int
)

declare @sell_info table
(
    name        varchar(6),
    sold        int
)

-- Insert sample data
insert into @sell_record select 'Josh', 1, 100, 0
insert into @sell_record select 'Jack', 1, 100, 0
insert into @sell_record select 'Josh', 2, 100, 0
insert into @sell_record select 'Austin', 1, 100, 0

insert into @sell_info select 'Josh', 150
insert into @sell_info select 'Jack', 0
insert into @sell_info select 'Austin', 0

-- The query. rcte is recursive cte
; with rcte as
(
    -- anchor member. Starts with package 1
    select  i.name, r.package,
            -- allocation of sold_qty to package
            alloc    = case when i.sold >= r.max_cap - r.sold 
                            then r.max_cap - r.sold 
                            else i.sold
                            end,
            -- balance of the sold qty after allocated
            bal_sold = case when i.sold >= r.max_cap - r.sold 
                            then i.sold - r.max_cap - r.sold 
                            else 0
                            end
    from    @sell_info i
            inner join @sell_record r   on  i.name  = r.name 
    where   r.package   = 1

    union all

    -- recursive member
    select  c.name, r.package,
            -- allocation of sold_qty to package
            alloc   = case  when c.bal_sold >= r.max_cap - r.sold 
                            then r.max_cap - r.sold 
                            else c.bal_sold
                            end,
            -- balance of the sold qty after allocated
            bal_sold = case when c.bal_sold >= r.max_cap - r.sold 
                            then c.bal_sold - r.max_cap - r.sold 
                            else 0
                            end
    from    rcte c
            inner join @sell_record r   on  c.name      = r.name 
                                        and c.package   = r.package - 1
)
-- Update back sell_record
update  r
set    sold = c.alloc
from    rcte c
        inner join @sell_record r   on  c.name  = r.name
                                    and c.package   = r.package

-- show the result
select  *
from    @sell_record

/*
name    package max_cap sold
Josh    1   100 100
Jack    1   100 0
Josh    2   100 50
Austin  1   100 0
*/