经典模式

简洁实用,快捷灵活

当前位置:首页>平台开发>经典模式
全部 1208 平台特色 8 经典模式 77 流行模式 108 技术论坛 1014 经典视频 1

SQL先进先出语句

时间:2022-05-07   访问量:3344

SQL先进先出语句 

很经典的“先进先出”的库存处理的SQL语句。 

CREATE TABLE #tmp  ( ID int IDENTITY (1, 1),

                        单价 decimal(18, 2) NOT NULL ,

                        数量 decimal(18, 2) NOT NULL ,

                        已出数量 decimal(18, 2) NOT NULL

                   )

insert into #tmp(单价,数量,已出数量) values(1.1,50,0)

insert into #tmp(单价,数量,已出数量) values(1.3,30,0)

insert into #tmp(单价,数量,已出数量) values(1.4,60,0)

insert into #tmp(单价,数量,已出数量) values(1.5,20,0)


declare @t decimal(18, 2),@temp decimal(18, 2)

set @t=90

update #tmp set @temp=case when @t>数量 then 数量 else @t end,数量=数量-@temp,@t=@t-@tempfrom #tmp where 单价<=1.30select * from #tmp 

go

drop table #tmp 


CREATE TABLE #tmp  (

                  ID int IDENTITY (1, 1),

                  单价 decimal(18, 2) NOT NULL ,

                  数量 decimal(18, 2) NOT NULL ,

                  已出数量 decimal(18, 2) NOT NULL

                    )

insert into #tmp(单价,数量,已出数量) values(1.1,50,0)

insert into #tmp(单价,数量,已出数量) values(1.3,30,0)

insert into #tmp(单价,数量,已出数量) values(1.4,60,0)

insert into #tmp(单价,数量,已出数量) values(1.5,20,0)

declare @t decimal(18, 2),@temp decimal(18, 2),@a varchar(8000)

select @t=60,@a=''

update #tmp set @temp=case when @t>数量-已出数量 then 数量-已出数量 else @t end, @t=@t-@temp,@a=@a+','+cast(@temp as varchar(10)),已出数量=@temp+已出数量 where 已出数量<>数量

select * from #tmp

set @t=30

update #tmp set @temp=case when @t>数量-已出数量 then 数量-已出数量 else @t end,@t=@t-@temp,@a=@a+','+cast(@temp as varchar(10)),已出数量=@temp+已出数量 where 已出数量<>数量

select * from #tmp

select right(@a,len(@a)-1)

go

drop table #tmp

 


上一篇:bs调用模板说明

下一篇:SQL简单入门资料

发表评论:

评论记录:

未查询到任何数据!

在线咨询

点击这里给我发消息 售前咨询专员

点击这里给我发消息 售后服务专员

在线咨询

免费通话

24小时免费咨询

请输入您的联系电话,座机请加区号

免费通话

微信扫一扫

微信联系
返回顶部
备案号码:鲁ICP备09000001号-2