1 Create TableT (2Idint Identity(1,1), namevarchar( -),--Product Name3Jint,--Inbound Quantity4Cint,--number of outbound libraries5JDatedatetime --Storage Time6 ) 7 Insert intoT (name,j,c,jdate)Select 'A', -,0,'2007-12-01' 8 Insert intoT (name,j,c,jdate)Select 'A', $,0,'2008-01-07' 9 Insert intoT (name,j,c,jdate)Select 'B', the,0,'2007-12-21' Ten Insert intoT (name,j,c,jdate)Select 'A', -,0,'2008-01-15' One Insert intoT (name,j,c,jdate)Select 'B', -,0,'2008-02-03' A Insert intoT (name,j,c,jdate)Select 'A',460,0,'2008-02-01' - Insert intoT (name,j,c,jdate)Select 'A',510,0,'2008-03-01' - Go
Create procwsp@name varchar( -),--Product Name@cost int --Sales Volume as --whether the stock of the goods is sufficientDeclare @spare float --remaining stockSelect @spare=sum(j)-sum(c) fromTwhereName=@name if(@spare>=@cost) begin --according to the date of entry into the first-out principle of the goods inventory processing UpdateTSetC= Case when(Select @cost-IsNull(sum(j),0)+IsNull(sum(c),0) fromTwhereName=@name andJDate<=A.jdate andJ!=C>=0 ThenA.JElse Case when(Select @cost-IsNull(sum(j),0)+IsNull(sum(c),0) fromTwhereName=@name andJDate<A.jdate andJ!=C<0 Then 0 Else(Select @cost-IsNull(sum(j),0)+IsNull(sum(c),0)+A.c fromTwhereName=@name andJDate<A.jdate andJ!=c)End End fromT AwhereName=@name andJ!=CEnd Else RAISERROR('Insufficient Inventory', -,1) return Go
exec @name = ' A ',@cost=
SQL uses FIFO stored procedure to find out the number of libraries