473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
3 13802
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joe | last post by:
Hey, I'm going to give some background on my situation in case anyone can point out a way around my problem altogether... for the problem itself, please skip to the bottom of the post. thanks....
1
by: Thomas Bartkus | last post by:
The meaning of REPLACE INTO is clear to me. IF the new record presents new key values, then it is inserted as a new record. IF the new record has key values that match a pre-existing record, then...
6
by: pk | last post by:
Sorry for the piece-by-piece nature of this post, I moved it from a dormant group to this one and it was 3 separate posts in the other group. Anyway... I'm trying to bulk insert a text file of...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
9
by: Curtis Stanford | last post by:
I'm in a situation where I need to load an ASCII file into a database. No sweat so far right? The records are indexed by date and I have a unique index on date. The ASCII can overlap, meaning it...
0
by: c_kubie | last post by:
I have an update string getting a list of names that are list1 but not in list2. (Sorry for the lame example) list1 ---- Bill Bill mike tom jim
1
by: cefrancke | last post by:
I'm trying to insert records in a table that has a "no duplicates" index on two columns. The following snippet shows where I started off... INSERT INTO Table_A (ID_A, ID_B) VALUES (334, 2057)...
2
by: Danyork | last post by:
Hello, I'm relatively new to VBA, and was wondering if someone would help me debug the code below. I want to put it in a form, so that when I click "OK" it inserts a new record into the...
4
code green
by: code green | last post by:
I am updating a table 'price_hist' inside a stored procedure in which I have placed a unique key constraint across three fields. In MySql the INSERT IGNORE command is useful on bulk inserts when a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.