SQL uses FIFO stored procedure to find out the number of libraries

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.