473,795 Members | 2,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14015
Use the MERGE statement. It's designed for exactly what you want.

"shorti" <lb******@juno. com> wrote in message
news:11******** **************@ e56g2000cwe.goo glegroups.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
2670
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. I've been having some problems with database performance... Several threads are constantly attempting INSERTs of new records into a large table - that is hundreds of thousands of records -large by my account :-) The table has a VARCHAR field...
1
34783
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 the key violation is ignored and the new record *replaces* the pre-existing record. But what about INSERT IGNORE? Is this a synonym for REPLACE INTO - i.e. it does precisely the same thing?
6
12358
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 10 columns into a table with 12. How can I specify which columns to insert to? I think format files are what I'm supposed to use, but I can't figure them out. I've also tried using a view, as was suggested on one of the many websites I've...
16
17021
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 must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
9
2132
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 can contain duplicate dates that have been loaded before. I started out with this ingenious idea. Just try to insert the record. If I get an error about duplicate keys, do an update and switch to update mode. If I'm updating and get zero rows...
0
422
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
15515
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) However, if these two values exist already as a "key" combination then, the MS Access
2
1636
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 ActionItems table. I only want to insert the one field "Review_ID" which is a foreign key, although the table has many other fields. The primary key in the table is ActionItem_ID, and is an Autonumber. The primary key is the only field indexed with "Yes...
4
33416
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 duplicate key is encountered as it prevents the query failing. How is similar achieved in MsSql?
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.