「先進先出」的庫存處理

2021-09-25 14:43:52 字數 1773 閱讀 4989

很經典的「先進先出」的庫存處理的sql語句。

create table #tmp --建立臨時表#tmp

( id int identity (1, 1), --建立列id,並且每次新增一條記錄就會加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) --declare申明區域性變數t 18位小數,保留小數點後4位即有13位整數,作用於begin和end之間即當前儲存過程中

set @t=90

update #tmp set @temp=case when @t>數量 then 數量 else @t end,數量=數量-@temp,@t=@t-@temp --@t:需要出庫數量,@temp:已出庫數量

from #tmp

where 單價<=1.30

select * from #tmp

godrop table #tmp --銷毀臨時表#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) --a最多可儲存8000個字元

elect @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)

godrop table #tmp

SQL處理庫存與銷售,先進先出原則

庫存表 create table t id intidentity 1 1 name varchar 50 商品名稱 j int 入庫數量 c int 出庫數量 jdate datetime 入庫時間 insert into t name,j,c,jdate select a 100,0,2007 ...

佇列 先進先出

看前面的是什麼型別 指標 普通 出現指標指向空報錯的情況下,傳參不能為空,可以傳個空間的位址給他 queue front next null 從尾進 先進先出 define crt secure no warnings pragma once include include include incl...

智慧型出庫,先進先出

3分鐘左右 t erp location 庫存表 4000行 如果是40000呢 code time location number 2000 20180101 a1 1 10 2000 20170102 b1 1 20 2000 20180103 c1 1 5 2000 20180101 d1 1...