Home > Enterprise >  Insert data from one table to another table
Insert data from one table to another table

Time:12-20

I want to insert data from one table to another but my issue is if I run SP second time it should not insert which data is already present KEY is Productcode

i should use joins(no syubquery)

INSERT INTO ProductCategoryMapping2
                    ( [ProductCode]
                    , [ProductName]
                    , [Category]
                    , [IsActive]
                    , [Division]
                    , [SubCategory])
        SELECT  TRIM(STR(MATCODE, 25, 0)) AS MATCODE
                    , MATNAME
                    , [OPERATIONS_PROD_CATEGORY_NAME]
                    ,IIF([MAT_BRAND_STATUS_NAME] = 'Active',1,0) as [IsActive]
                    ,[OPERATIONS_PROD_DIVISION_NAME]
                    , [OPERATIONS_PROD_SUBCATEGORY_NAME]
                    FROM [dbo].[Sheet1$] s

this code insert all data to destination table each and every time

CodePudding user response:

Since you only need to find new data, not present in the destination table, you have to add a left join with the destination table and filter by key=NULL

This is not a working example but can guide you to write the actual query:

LEFT JOIN ProductCategoryMapping2 destination ON s.key = destination.key
WHERE destination.key IS NULL

We are aiming for the second left join in this picture.

enter image description here

  • Related