SQL Simple analysis of MRP resources

Source: Internet
Author: User

Wrote a simple MRP based on the data passed in, the judgment of the subordinate, the parent table, the child table

Idea:

3 variables table, the first to save the incoming product, this side of the table do a loop is the finished product is the same as in, take the first row of the finished table, the data as the parent table and the Child Table Association, take out the related items, save to the second variable table.

Make a loop again, associate the second variable table with the parent table, if there is data stored in the third variable table, delete the same data from the second variable table according to the third variable table, then associate the third variable table with the parent table sub-table, reverse the data back to the second variable table, and empty the third variable table. Loop to no condition to be established, can.

Here's My method:

  

----------------Begin----------------------------
Declare @wltemp table (ID int,code varchar (50))
Insert into @wltemp (Id,code)
Select ' 1 ', ' par-471j0-60 '
--union
--select ' 2 ', ' par-102j0-60 '

Declare @temp table (FID Int,mainid int,num int,whscode int)
Declare @temp1 table (FID Int,mainid int,num Int,whscode int)
DECLARE @i int
Declare @maxvalue int
Select @maxvalue =count (1) from @wltemp
Select @i=1
while (@i& Lt [email protected])
Begin
If exists (select top 1 1 from dchild1727 a inner joins dfather1727 B on A.fid=b.id
Inner joins DITM C on B. Mainid=c.id INNER JOIN @wltemp D on C.fathercode=d.code where [email protected])
begin
INSERT INTO @temp (FID, Mainid,num,whscode)
Select A.fid,a.mainid,a.c_num,a.whscode from dchild1727 a inner joins dfather1727 B on a.fid=b.id Br>inner join DITM C on b.mainid=c.id inner join @wltemp D on C.fathercode=d.code where [email protected]
End
S ET @[email protected]+1

The upper part of the-------------------------is--------------------the next part of the finished name is Loop-------------------------------
While exists (select top 1 1 from dfather1727 a inner joins @temp B on A.mainid=b.mainid)
Begin
Insert into @temp1 (Fid,mainid,num,whscode)
Select B.fid,b.mainid,b.num,b.whscode from dfather1727 a inner joins @temp B on A.mainid=b.mainid

Delete from @temp where Mainid in (select Mainid from @temp1 a where A.mainid=mainid and A.fid=fid)

Insert into @temp (Fid,mainid,num,whscode)
Select A.fid,a.mainid,a.c_num,a.whscode
From dchild1727 a inner joins dfather1727 B on a.fid=b.id inner joins @temp1 C on B.mainid=c.mainid
Delete from @temp1
End
End

SELECT B.fathercode Item code, b.itemname item name, A.whscode Warehouse, a.num quantity from @temp a inner join DITM B on a.mainid=b.id
---------------End-------------

SQL Simple analysis of MRP resources

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.