简体   繁体   中英

Insert data from one table to another table

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

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.

在此处输入图像描述

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