?
Solved

Insert new records only

Posted on 2007-11-20
9
Medium Priority
?
724 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 80 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
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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 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.
Suggested Courses

770 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