By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,288 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,288 IT Pros & Developers. It's quick & easy.

INSERT but ignore duplicates

P: n/a
I am looking for a way to insert into one table from another table but
ignore duplicates (because the query will fail since the column I am
inserting into is a unique index).

RE:

INSERT INTO table1 (SELECT column1 from table2)

This query works if table1 is empty but if table1 has any recorda they
would be duplicate to table2. I also cannot delete the existing
records in table1. I did not see anything like an IGNORE clause and I
really would like to avoid opening my own cursor to go through the
table. Any suggestions?

May 5 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Use the MERGE statement. It's designed for exactly what you want.

"shorti" <lb******@juno.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
I am looking for a way to insert into one table from another table but
ignore duplicates (because the query will fail since the column I am
inserting into is a unique index).

RE:

INSERT INTO table1 (SELECT column1 from table2)

This query works if table1 is empty but if table1 has any recorda they
would be duplicate to table2. I also cannot delete the existing
records in table1. I did not see anything like an IGNORE clause and I
really would like to avoid opening my own cursor to go through the
table. Any suggestions?

May 5 '06 #2

P: n/a
> I did not see anything like an IGNORE clause

WHERE means ignore anything that doesn't satisfy the clause.

INSERT INTO table1 (SELECT DISTINCT column1 from table2
WHERE NOT EXISTS (SELECT * FROM table1 WHERE column1 = table2.column1))

Though, you could also use MERGE.

B.

May 5 '06 #3

P: n/a
Ok..thanks! I will try them out!

May 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.