troubleshooting Question

Insert into SQL Table where Pkey doesn't exist.

Avatar of mburk1968
mburk1968Flag for United States of America asked on
SQL
2 Comments1 Solution72 ViewsLast Modified:
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;
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros