Rains
Rains

Reputation: 57

Inserting Non duplicate data into a table from Parent table

I have 2 tables : OSUSR_1SV_STAGING_FTP and #OSUSR_1SV_STAGING_FTP1

with same columns on both the table as :

Customer_Part_Number,
Lear_Part_Number,
Shipping_ID,
Customer_Name,
Effective_Date,
End_Date,Change_ID,
PO_Number,
PO_Price

I have successfully copied all the data to table #OSUSR_1SV_STAGING_FTP1 from table OSUSR_1SV_STAGING_FTP.
But my scenario stucks when the records are same in table OSUSR_1SV_STAGING_FTP and I don't want it to get it inserted to table #OSUSR_1SV_STAGING_FTP1. Just need to insert non duplicate records.

Upvotes: 0

Views: 28

Answers (2)

TheGameiswar
TheGameiswar

Reputation: 28900

This inserts records only which are in OSUSR_1SV_STAGING_FTP but not in OSUSR_1SV_STAGING_FTP1

INSERT INTO OSUSR_1SV_STAGING_FTP1
SELECT * FROM OSUSR_1SV_STAGING_FTP 
EXCEPT
SELECT * FROM OSUSR_1SV_STAGING_FTP1

Upvotes: 1

Isaac
Isaac

Reputation: 3363

First off you need to have a unique id for each row. Then you can select from Table 1 (OSUSR_1SV_STAGING_FTP) and insert into Table 2 (#OSUSR_1SV_STAGING_FTP1) where it doesn't already exist.

INSERT INTO #OSUSR_1SV_STAGING_FTP1 SELECT * FROM OSUSR_1SV_STAGING_FTP WHERE OSUSR_1SV_STAGING_FTP.[Unique_ID] NOT IN (SELECT Unique_ID FROM #OSUSR_1SV_STAGING_FTP1)

Noel

Upvotes: 0

Related Questions