467,189 Members | 1,173 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,189 developers. It's quick & easy.

INSERT but ignore duplicates

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
  • viewed: 11031
Share:
3 Replies
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
> 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
Ok..thanks! I will try them out!

May 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Thomas Bartkus | last post: by
6 posts views Thread by pk | last post: by
16 posts views Thread by Philip Boonzaaier | last post: by
reply views Thread by c_kubie@yahoo.com | last post: by
1 post views Thread by cefrancke@yahoo.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.