Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Insert new records only

Posted on 2007-11-20
9
Medium Priority
?
727 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

636 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