Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

NEWBIE: Any way to ignore duplicates on INSERT?

Posted on 2006-10-31
5
Medium Priority
?
3,618 Views
Last Modified: 2009-10-23
Dear Experts,

I'm trying to insert records from one table into another table with the same key structure.  I'm using:

INSERT INTO...SELECT...FROM

So the entire insert takes place in a single statement.

But it fails if even a single record already exists in both tables.

Is there any way to say "If there's a duplicate, OVERWRITE the OLD with the NEW"?

If not, is there any way to say, "If there's a duplicate, ignore it"?

If not that either, what do I do?  Throw myself off a bridge?  How high?

Thanks,
BrianMc1958
 
0
Comment
Question by:BrianMc1958
[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
5 Comments
 
LVL 9

Expert Comment

by:nito8300
ID: 17844426
create an index and ignore duplicate values.

in EM, right click the table, go to all tasks, manage indexes add new index, select your primary key and set it to ignore duplicates...
0
 
LVL 7

Accepted Solution

by:
mikkilineni earned 1400 total points
ID: 17844431
INSERT INTO table 1 SELECT col1,col2 ,col3...FROM table2 where primartkeycol not in (select  primarykeycol from table1)
0
 

Author Comment

by:BrianMc1958
ID: 17844600
To nito8300:  Thanks, but I should have told you I need to do this with TSQL, programatically.

To mikkilineni:  Thanks even more.  That will basically solve my problem.  But is there any reasonably simple way to have the NEW row inserted?  I believe your solution will retain the OLD row, right?
0
 
LVL 1

Assisted Solution

by:asduth
asduth earned 600 total points
ID: 17844792
There is no very simple way to achieve this.

You have 2 basic choices - either delete the rows, then do the insert,

e.g.

delete table1 from table1 join table1 on table1.PK = table2.PK
insert into table1 select * from table2

or else, do an update of the existing rows followed by the selective insert:

update table1 set
table1.col1 = table2.col1,
table1.col2 = table2.col2
from table2 where table1.PK = table2.PK

insert into table1
select * from table2
where table2.PK not in (select PK from table1)


Most likely, the update/insert will be better performance than the delete/insert. Particularly if you have large data sets and/or you have a clustered index on the PK.

If you are doing this from many places, you could think of encapsulating it in a trigger on the table.

0
 

Author Comment

by:BrianMc1958
ID: 17844814
Thanks 1,000,000.00, folks.
--BrianMc1958
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

660 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