To achieve the requirment of loading data from Source table to Target table with ensuring it will pull only data which is not present in Target table, we need to use JOIN and WHERE Condition to achieve it. Here we are going to use LEFT JOIN & WHERE condition with IS NULL.
Sample code:
--Source Table
SELECT *
FROM MyProduct
--Target Table
SELECT *
FROM MyProductHistory
--INSERT INTO MyProductHistory
SELECT P1.*
FROM MyProduct P1
LEFT JOIN MyProductHistory P2 ON p1.ProductID = p2.ProductID
WHERE p2.productID IS NULL
Query Execution Explaination:
1) This query will pull all the matching ProductID from MyProduct table (Source table) and MyProductHistory table (Target Table)
2) This query will pull all the new products from MyProduct table (Source table)
3) At last, It will filter data based on Products not available in MyProductHistory table (Target Table)
4) In this way, Output will be only products which are not available in MyProductHistory table (Target Table)
No comments:
Post a Comment