473,804 Members | 4,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

INSERT IGNORE duplicates

code green
1,726 Recognized Expert Top Contributor
I am updating a table 'price_hist' inside a stored procedure in which I have placed a unique key constraint across three fields.
price_list, part_no, price
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?
Mar 24 '09 #1
4 33416
ck9663
2,878 Recognized Expert Specialist
IGNORE meaning, not insert or insert still but not give error?

-- CK
Mar 24 '09 #2
code green
1,726 Recognized Expert Top Contributor
I do not want to insert duplicate records,
but I don't want the procedure failing if a duplicate is encountered.
So the query needs to IGNORE the error, skip the offending record and continue.
Here is part of the procedure so far
DECLARE @lastdate AS DATETIME
SELECT @lastdate = MAX(date_from)F ROM plis_hist;
SET DATEFIRST 3 -- Set the first day of the week to Wednesday

/* Now copy prices from plis to plis_hist */
INSERT INTO plis_hist
(price_list, part, date_from, volume,dt_creat ed, last_upd_user,
price, week_no)
(
SELECT pls.price_list, pls.part, pls.date_from, pls.volume,
pls.dt_created, pls.last_upd_us er, pls.price,
dbo.FiscalWeek( '01',pls.date_f rom) week_no
FROM plis
LEFT JOIN plis_hist hist
ON (hist.part = pls.part
AND hist.price_list = pls.price_list
AND (hist.price != pls.price OR hist.part IS NULL))
AND pls.date_from > @lastdate
);
Unfortunately it is possible for users to update the plis table when nothing but the date_from changes so price_list, part, volume, price remain the same.
As these are my CONSTRAINT fields a CONSTRAINT error is thrown
Mar 24 '09 #3
ck9663
2,878 Recognized Expert Specialist
I did not touch your first JOIN and just add a new one. The where clause will filter out those that are already existing ie the same values with those CONSTRAINT fields

Expand|Select|Wrap|Line Numbers
  1.  
  2. INSERT INTO plis_hist
  3. (price_list, part, date_from, volume,dt_created, last_upd_user, price, week_no)
  4. (
  5. SELECT pls.price_list, pls.part, pls.date_from, pls.volume,
  6. pls.dt_created, pls.last_upd_user, pls.price,
  7. dbo.FiscalWeek('01',pls.date_from) week_no
  8. FROM plis
  9. LEFT JOIN plis_hist hist
  10.    ON (hist.part = pls.part AND hist.price_list = pls.price_list AND (hist.price != pls.price OR hist.part IS NULL)) AND pls.date_from > @lastdate
  11. left join plis_hist hist2
  12.    on (plis.part = hist2.part and plis.price_list = hist2.price_list and plis.volume = hist2.volume and plis.price = hist2.price)
  13. where hist2.part is null
  14. );
  15.  
  16.  
Mar 24 '09 #4
code green
1,726 Recognized Expert Top Contributor
Thanks for that idea ck9663.
I was thinking along similar lines using a
WHERE NOT IN (SELECT price_list,part ,price FROM ...) sub-query.
But your JOIN is much better performance wise.
Mar 25 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2671
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
34800
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
12359
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
17026
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
3
14020
by: shorti | last post by:
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
1
15517
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
1639
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...
0
9585
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,...
0
10586
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9161
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
7622
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
6856
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
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2997
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.