Solved

Insert new records only

Posted on 2007-11-20
9
715 Views
Last Modified: 2008-02-15
I'm trying to insert only new records into a table and wanted to know if this was the best solution.  I know if I was only comparing one field in the different tables I could do a not in (select distinct etc...) but the new value is based on two fields creating the unique value.  This is the query I'm considering using.  Any better ways of doing this?


INSERT INTO SGroups(DepartmentID, SGroupName)
SELECT DISTINCT A.Department, A.SGroup
FROM EmpData_Dump A LEFT JOIN SGroups B ON A.Department = B.DepartmentID and A.SGroup = B.SGroupName
WHERE B.DepartmentID IS NULL AND B.SGroupName is null
0
Comment
Question by:tim_cs
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20320432
This is absolutely good. Nothing better than this.
0
 
LVL 18

Expert Comment

by:Yveau
ID: 20320455
That is exactly what I would do ...

Hope this helps ...
0
 
LVL 18

Assisted Solution

by:Sham Haque
Sham Haque earned 20 total points
ID: 20320482
another way:


INSERT INTO SGroups(DepartmentID, SGroupName)

SELECT DISTINCT A.Department, A.SGroup

FROM EmpData_Dump A 

WHERE NOT EXISTS

(SELECT 1

FROM SGroups B 

WHERE A.Department = B.DepartmentID and A.SGroup = B.SGroupName)

Open in new window

0
 
LVL 15

Author Comment

by:tim_cs
ID: 20320535
Thanks to everybody for the feedback and thank you for the other solution gbshahaq.  Is there a reason to do it that way versus the other method?  Which would be faster for larger tables?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20320545
for larger table your query would be a good performer.
0
 
LVL 18

Expert Comment

by:JR2003
ID: 20320709
As you're using SQL Server 2005 you can make use of the new EXCEPT set operator. This is sometimes quicker for large result sets.

INSERT INTO SGroups(DepartmentID, SGroupName)

SELECT DISTINCT A.Department, A.SGroup

FROM EmpData_Dump A 

EXCEPT 

SELECT DISTINCT B.DepartmentID, B.SGroupName

Open in new window

0
 
LVL 18

Expert Comment

by:JR2003
ID: 20320711
should have said


INSERT INTO SGroups(DepartmentID, SGroupName)

SELECT DISTINCT A.Department, A.SGroup

FROM EmpData_Dump A 

EXCEPT 

SELECT DISTINCT B.DepartmentID, B.SGroupName

FROM EmpData_Dump B

Open in new window

0
 
LVL 18

Accepted Solution

by:
JR2003 earned 45 total points
ID: 20320728
What I really meant to put was:
INSERT INTO SGroups(DepartmentID, SGroupName)

SELECT DISTINCT A.Department, A.SGroup

FROM EmpData_Dump A 

EXCEPT 

SELECT DISTINCT B.DepartmentID, B.SGroupName

FROM SGroups B

Open in new window

0
 
LVL 18

Expert Comment

by:Yveau
ID: 20322393
:-) Three times is a charm !
Good pointJR2003, I came back to the thread to inform tim_cs on this option, but you beat me to it :-)
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now