34,593
社区成员
发帖
与我相关
我的任务
分享
--库存表
/*
TB1
SKU BatchNo Qty OrdQty Volume
180050 DY101110 1 1 0.0351
180050 DY111117 1 1 0.075
180050 DY111108 18 9 2.5
180050 DY111109 7 4 1.40832
*/
TB2
--发货表
/*
SKU Qty
180050 20
*/
--更新后库存表
/*
SKU BatchNo Qty OrdQty Volume
180050 DY101110 0 1 0.0351
180050 DY111108 0 9 2.5
180050 DY111109 6 4 1.40832
180050 DY111117 1 1 0.075
*/
--求一更新语句,BatchNo字段升序排序后,UPDATE->QTY字段
--半把操作的库存表的字段插入后TB3
/*
TB3
SKU BatchNo Qty OrdQty Volume
180050 DY101110 1 1 0.0351
180050 DY111108 18 9 2.5
180050 DY111109 1 4 1.40832
*/
declare @cnt int, @tmp int
set @cnt = 0
set @tmp = 0
declare @TB1 table
(SKU int,BatchNo varchar(8),Qty int,OrdQty int,Volume numeric(6,5))
insert into @TB1
select 180050,'DY101110',1,1,0.0351 union all
select 180050,'DY111117',1,1,0.075 union all
select 180050,'DY111108',18,9,2.5 union all
select 180050,'DY111109',7,4,1.40832
declare @TB2 table
(sku int,qty int)
insert into @TB2
select 180050,22
update t1 set @cnt = @cnt + t3.Qty, @tmp = t2.qty - @cnt, t1.Qty = case when @tmp >= 0 then 0 else -@tmp end
from (select top 100 percent * from @TB1 order by BatchNo) t3 inner join @TB1 t1 on t1.BatchNo = t3.BatchNo inner join @TB2 t2 on t3.SKU = t2.sku
select * from @TB1
--> 测试数据:[TB1]
if object_id('[TB1]') is not null drop table [TB1]
go
create table [TB1]([SKU] int,[BatchNo] varchar(8),[Qty] int,[OrdQty] int,[Volume] numeric(6,5))
insert [TB1]
select 180050,'DY101110',1,1,0.0351 union all
select 180050,'DY111117',1,1,0.075 union all
select 180050,'DY111108',18,9,2.5 union all
select 180050,'DY111109',7,4,1.40832
--------------开始查询--------------------------
--> 测试数据:[TB2]
if object_id('[TB2]') is not null drop table [TB2]
go
create table [TB2]([SKU] int,[Qty] int)
insert [TB2]
select 180050,20
--------------开始查询--------------------------
; with t as
(
select *,id=row_number() over(order by BatchNo) from[TB1]
)
update a set
a.Qty= case when b.[Qty]>(select sum([Qty]) from t where [id]<=a.[id])
then 0
when b.[Qty]>(select sum([Qty]) from t where [id]<a.[id])
and b.[Qty]<(select sum([Qty]) from t where [id]<=a.[id]+1)
then a.[Qty]-(b.[Qty]-(select sum([Qty]) from t where [id]<a.[id]))
else a.[Qty]
end
from t a,[TB2] b
select * from tb1 order by BatchNo
/*
SKU BatchNo Qty OrdQty Volume
----------- -------- ----------- ----------- ---------------------------------------
180050 DY101110 0 1 0.03510
180050 DY111108 0 9 2.50000
180050 DY111109 6 4 1.40832
180050 DY111117 1 1 0.07500
(4 行受影响)
*/
create table tb
(SKU int,BatchNo varchar(8),Qty int,OrdQty int,Volume numeric(6,5))
insert into tb
select 180050,'DY101110',1,1,0.0351 union all
select 180050,'DY111117',1,1,0.075 union all
select 180050,'DY111108',18,9,2.5 union all
select 180050,'DY111109',7,4,1.40832
go
create table tb2(sku int,qty int)
insert into tb2
select 180050,20
go
declare @batch varchar(max)
declare @qty int
declare @newqty int
;with ach as
(
select a.*,b.qty newqty
from tb a join tb2 b on a.sku = b.sku
where (select sum(qty) from tb where sku=a.sku and BatchNo<=a.BatchNo) >= b.qty
and (select sum(qty) from tb where sku=a.sku and BatchNo<a.BatchNo) <= b.qty
)
update a
set @qty=(select sum(qty) from tb where sku=a.sku and BatchNo<=a.BatchNo),
@newqty = b.newqty - @qty,
a.qty = (case when @newqty<=0 then @qty-b.newqty
when @newqty>0 then 0 end)
from tb a join ach b on a.sku = b.sku and a.BatchNo<=b.BatchNo
select * from tb
drop table tb,tb2
/**************************
SKU BatchNo Qty OrdQty Volume
----------- -------- ----------- ----------- ---------------------------------------
180050 DY101110 0 1 0.03510
180050 DY111117 1 1 0.07500
180050 DY111108 0 9 2.50000
180050 DY111109 6 4 1.40832
(4 行受影响)
--多个编号的更新
--测试数据表1(我把主键id加上了)
go
create table bitlstb1
(id int primary key,sku int,batchno varchar(8),qty int,ordqty int,volume numeric(6,5))
insert into bitlstb1
select 1,180050,'dy101110',1,1,0.0351 union all
select 2,180050,'dy111117',1,1,0.075 union all
select 3,180050,'dy111108',18,9,2.5 union all
select 4,180050,'dy111109',7,4,1.40832 union all
select 5,180051,'dy101110',3,1,0.0351 union all
select 6,180051,'dy111117',2,1,0.075 union all
select 7,180051,'dy111108',18,9,2.5 union all
select 8,180051,'dy111109',7,4,1.40832
go
--测试数据表2
create table bitlstb2(sku int,qty int)
insert into bitlstb2
select 180050,20 union all
select 180051,10
go
--创建一个存储过程
create proc updateqty(@p int)
as
begin
--得到发货数量
declare @qty int
select @qty=qty from bitlstb2 where sku=@p
declare @j varchar(20);declare @k int
--逐行更新
;with maco as ( select * from bitlstb1 where sku=@p),
maco1 as(select *,zqty=(select sum(qty) from maco where batchno<=a.batchno) from maco a)
select top 1 @j=batchno,@k=zqty-@qty from maco1 where zqty>=@qty order by batchno
update bitlstb1 set qty=@k where batchno=@j and sku=@p
update bitlstb1 set qty=0 where sku=@p and batchno<@j and sku=@p
end
declare @sql varchar(max) set @sql=''
select @sql=@sql+' exec updateqty '+ltrim(sku)+';' from bitlstb2
exec(@sql)
select * from bitlstb1
/*
id sku batchno qty ordqty volume
----------- ----------- -------- ----------- ----------- ---------------------------------------
1 180050 dy101110 0 1 0.03510
2 180050 dy111117 1 1 0.07500
3 180050 dy111108 0 9 2.50000
4 180050 dy111109 6 4 1.40832
5 180051 dy101110 0 1 0.03510
6 180051 dy111117 2 1 0.07500
7 180051 dy111108 11 9 2.50000
8 180051 dy111109 7 4 1.40832
*/
drop table bitlstb1,bitlstb2
--修正一下
--测试数据表1(我把主键id加上了)
declare @tb1 table
(id int primary key,sku int,batchno varchar(8),qty int,ordqty int,volume numeric(6,5))
insert into @tb1
select 1,180050,'dy101110',1,1,0.0351 union all
select 2,180050,'dy111117',1,1,0.075 union all
select 3,180050,'dy111108',18,9,2.5 union all
select 4,180050,'dy111109',7,4,1.40832
--测试数据表2
declare @tb2 table (sku int,qty int)
insert into @tb2
select 180050,20
--设置参数
declare @p int set @p=180050
--得到发货数量
declare @qty int
select @qty=qty from @tb2 where sku=180050
declare @j VARCHAR(20);declare @k int
--逐行更新
;with maco as ( select * from @tb1 where sku=180050),
maco1 as(select *,zqty=(select sum(qty) from maco where batchno<=a.batchno) from maco a)
select top 1 @j=batchno,@k=zqty-@qty from maco1 where zqty>=@qty order by batchno
update @tb1 set qty=@k where batchno=@j
update @tb1 set qty=0 where sku=180050 and batchno<@j
--查看结果(按生产批量排序)
select * from @tb1 order by 3
--结果
/*
id sku batchno qty ordqty volume
----------- ----------- -------- ----------- ----------- ---------------------------------------
1 180050 dy101110 0 1 0.03510
3 180050 dy111108 0 9 2.50000
4 180050 dy111109 6 4 1.40832
2 180050 dy111117 1 1 0.07500
*/
--测试数据表1
declare @tb1 table
(sku int,batchno varchar(8),qty int,ordqty int,volume numeric(6,5))
insert into @tb1
select 180050,'dy101110',1,1,0.0351 union all
select 180050,'dy111117',1,1,0.075 union all
select 180050,'dy111108',18,9,2.5 union all
select 180050,'dy111109',7,4,1.40832
--测试数据表2
declare @tb2 table (sku int,qty int)
insert into @tb2
select 180050,20
--设置参数
declare @p int set @p=180050
--可以加上条件例如where sku=180050,然后排序后放到临时表中
select * into #tb3 from @tb1 where sku=180050 order by batchno
--得到发货数量
declare @qty int
select @qty=qty from @tb2 where sku=180050
--逐行更新
update #tb3
set @qty = case when @qty < 0 then 0 else @qty - qty end ,
qty = case when @qty > 0 then 0 when @qty = 0 then qty else -@qty end
--把临时表的结果再更新到原表中,正常是主键关联(关联改成你的id主键即可)
update @tb1 set qty = b.qty from @tb1 a
left join #tb3 b on a.batchno = b.batchno
--查看结果(按生产批量排序)
select * from @tb1 order by 2
--结果
/*
sku batchno qty ordqty volume
----------- -------- ----------- ----------- ---------------------------------------
180050 dy101110 0 1 0.03510
180050 dy111108 0 9 2.50000
180050 dy111109 6 4 1.40832
180050 dy111117 1 1 0.07500
*/
drop table #tb3
SELECT * INTO #t FROM @TB1 ORDER BY BatchNo
DECLARE @i INT SET @i=20
update #t
set @i=case when @i<0 then 0 else @i-Qty end,
Qty=case when @i>0 then 0 when @i=0 then Qty else -@i END
UPDATE @TB1 SET Qty=b.Qty
FROM @TB1 a LEFT JOIN #t b ON a.BatchNo=b.BatchNo
select * from @TB1