troubleshooting Question

Simple insert where not exists statement assistance

Avatar of blberger
blberger asked on
Microsoft SQL ServerSQL
6 Comments1 Solution648 ViewsLast Modified:
I want to insert data from table1 into table2, where both contain the same fields, and table1 contains many duplicates.  (This table is cleared and re-populated daily).  I want to insert records from table1 into table2 and not allow a duplicate ID in table2.  For the purpose of this question, the fields in the tables are ID (int), FName varchar(20), LName varchar(20), State char(2), RecordID (int).  The query that I am trying to use is:

INSERT INTO [dbo].[table2] ([ID], [FName], [LName], [RecordID]) SELECT [ID], [FName], [LName], [RecordID] FROM [dbo].[table1] WHERE [ID] NOT EXISTS (SELECT [ID] FROM [dbo].[table2])

The result records in table2 contain duplicate ID values.

Any assistance would be appreciated.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 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 6 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