Insert into SQL Table where Pkey doesn't exist.

I have the following insert statement that I would like to run on a schedule. I want it to only insert records that have not already been inserted previously. The pkey field is my unique identifier.

INSERT  INTO [KLL_Cust].[dbo].[KLLStyleMasterException]
        ( [pkey] ,
		  [season] ,
          [Style] ,
          [lbl_code] ,
          [dimension] ,
		  [Date]
        )
        SELECT DISTINCT
		        L.pkey ,
                L.season ,
                RTRIM(L.style) Style ,
                lbl_code ,
                L.dimension ,
				GetDate() Datetime
        FROM    zzxscolr L
                LEFT OUTER JOIN ( SELECT DISTINCT
				                            pkey ,
                                            style ,
                                            dimension
                                  FROM      zzcordrd
                                  WHERE     total_qty <> 0
                                ) P ON P.style = L.style
                                       AND P.dimension = L.dimension
        WHERE   L.lbl_code <> ''
                AND L.style NOT IN ( SELECT style
                                     FROM   zzxscolr N
                                     WHERE  L.style = N.style
                                            AND L.dimension = N.dimension
                                            AND L.color_code = N.color_code
                                            AND N.lbl_code = '' )
                AND L.season <> 'ANY'
                AND division = 'KLL'
                AND season > '20171'
                AND P.style IS NOT NULL
	    END;

Open in new window

mburk1968Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Please try this -

INSERT  INTO [KLL_Cust].[dbo].[KLLStyleMasterException]
        ( [pkey] ,
		  [season] ,
          [Style] ,
          [lbl_code] ,
          [dimension] ,
		  [Date]
        )

		SELECT u.* FROM 
		(
				SELECT DISTINCT
						L.pkey ,
						L.season ,
						RTRIM(L.style) Style ,
						lbl_code ,
						L.dimension ,
						GetDate() Datetime
				FROM    zzxscolr L
						LEFT OUTER JOIN ( SELECT DISTINCT
													pkey ,
													style ,
													dimension
										  FROM      zzcordrd
										  WHERE     total_qty <> 0
										) P ON P.style = L.style
											   AND P.dimension = L.dimension
				WHERE   L.lbl_code <> ''
						AND L.style NOT IN ( SELECT style
											 FROM   zzxscolr N
											 WHERE  L.style = N.style
													AND L.dimension = N.dimension
													AND L.color_code = N.color_code
													AND N.lbl_code = '' )
						AND L.season <> 'ANY'
						AND division = 'KLL'
						AND season > '20171'
						AND P.style IS NOT NULL
			)u LEFT JOIN [KLL_Cust].[dbo].[KLLStyleMasterException] k ON u.pkey = k.pkey
			WHERE k.pkey IS NULL

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mburk1968Author Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.