简体   繁体   中英

returning rows from one table based on a searching from a column or another table and writing the rows to a third table

 SELECT PartList.cprj, PartList.PartNumber, PartList.SWITEM, PartList.QTY, ItemMaster.dsca, ItemMaster.item FROM SSIS.dbo.SWBOM as PartList INNER JOIN ItemMasterRoutingPOC.dbo.[Item Master Data] AS ItemMaster ON ItemMaster.item = PartList.PartNumber UNION ALL SELECT PartList.cprj, PartList.PartNumber, PartList.SWITEM, PartList.QTY, ItemMaster.dsca, ItemMaster.item FROM SWBOM PartList THIS SHOULD CHANGE BASED ON YOUR PART NUMBER SEQUENCE INNER JOIN ItemMasterRoutingPOC.dbo.[Item Master Data] AS ItemMaster ON ItemMaster.item = (CASE WHEN (ItemMaster.item=LEFT(ItemMaster.item,8) + '-XX') AND (ItemMaster.item=LEFT(ItemMaster.item,8) + '-X') THEN (LEFT(PartList.PartNumber,8) + '-XX') ELSE (LEFT(PartList.PartNumber,8) + '-X') END) LEFT JOIN ( SELECT PartList.PartNumber as FoundPartNumber FROM SWBOM PartList INNER JOIN ItemMasterRoutingPOC.dbo.[Item Master Data] AS ItemMaster ON ItemMaster.item = PartList.PartNumber ) IT ON IT.FoundPartNumber = PartList.PartNumber WHERE IT.FoundPartNumber IS NULL

I'm new to this programming. I have 2 tables in two different databases. In the one table I have a PartNumber column, which contains many numbers that I want to cross reference in another table in a different database. So for every entry in the PartNumber column (cycling thru them) I want to match to the PartNumber column on the 2nd database. They may not have to be exact matches or I could search for the first 8 characters. So for every PartNumber search I would like ALL the rows from the second table to be inserted into a third table that would be in the same database as the first table.

I have tried to write some code but with my limited knowledge now very successful. The code is as such:

IF OBJECT_ID('FFGD.dbo.BAANExport') IS NOT NULL
    DROP TABLE FFGD.dbo.BAANExport
GO

CREATE TABLE FFGD.dbo.BAANExport
(
    ITEM VARCHAR(512),
    PartNumber VARCHAR(512),
    QTY VARCHAR(512),
    dsca VARCHAR(512),
    Level VARCHAR(512),
    trid VARCHAR(512),
    cprj VARCHAR(512),
    mitm VARCHAR(512),
    pono VARCHAR(512),
    sitm VARCHAR(512),
    opol VARCHAR(512),
    qana VARCHAR(512),
    scpf VARCHAR(512),
    cwar VARCHAR(512),
    opno VARCHAR(512),
    cpha VARCHAR(512),
    exin VARCHAR(512),
    itlu VARCHAR(512),
    ssta VARCHAR(512),
)
GO


DECLARE PartNumber_cursor CURSOR FOR
SELECT PartNumber
FROM FFGD.dbo.ImportCSV;

OPEN PartNumber_cursor;
FETCH NEXT FROM PartNumber_cursor;

WHILE @@FETCH_STATUS = 0
   BEGIN
      FETCH NEXT FROM PartNumber_cursor;
      Insert into FFGD.dbo.BAANExport
      Select * from BAANItems.dbo.ItemMaster, FFGD.dbo.ImportCSV
      where BAANItems.dbo.ItemMaster.PartNumber = BAANItems.dbo.ItemMaster.PartNumber

   END;


CLOSE PartNumber_cursor;
DEALLOCATE PartNumber_cursor;
GO

BAANEXport is the third table to be written to. ImportCSV is the first table containing the part numbers I want to find in the second table. ItemMaster is the second table containing the rows I want to extract.

I have not attempted to do try and search with not an exact match. I was trying to get some kind of output with exact at this time.

I don't see where you need a cursor. You can join both tables in a single SELECT by using the full 3-part path, and then go directly into your table. To do a match where the Part Number begins with the same part number in the parent table, use a like with a wildcard.

Something along these lines:

Insert into FFGD.dbo.BAANExport
    (PartNumber)
SELECT
    PartList.PartNumber
FROM 
    FFGD.dbo.ImportCSV PartList
INNER JOIN
    BAANItems.dbo.ItemMaster ItemMaster ON PartList.PartNumber LIKE  BAANItems.dbo.ItemMaster.PartNumber + '%' 

You're going to need to do a UNION to first pull the exact matches, and then pull in the non matches that follow the pattern you need. Here is the basic idea. You will need to change the JOIN I have highlighted a remark on here. Is the 'NO2-XXXX' portion fixed, and then you have variations thereafter?

I would need to understand more about the exact sequence on part numbers to give you the exact SQL, but this should get you close.

SELECT
    PartList.PartNumber
FROM 
    ImportCSV PartList
INNER JOIN
    ItemMaster ON ItemMaster.PartNumber = PartList.PartNumber  

UNION ALL

SELECT
    ItemMaster.PartNumber
FROM 
    ImportCSV PartList
-- THIS SHOULD CHANGE BASED ON YOUR PART NUMBER SEQUENCE
INNER JOIN
    ItemMaster ON LEFT(ItemMaster.PartNumber,8) = LEFT(PartList.PartNumber,8) 
LEFT JOIN
(
    SELECT
        PartList.PartNumber as FoundPartNumber
    FROM 
        ImportCSV PartList
    INNER JOIN
        ItemMaster ON ItemMaster.PartNumber = PartList.PartNumber  

) IT ON IT.FoundPartNumber = PartList.PartNumber
WHERE
    IT.FoundPartNumber IS NULL   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM