Solved

Insert new records only

Posted on 2007-11-20
9
719 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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
 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

829 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