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?
4 33416 ck9663 2,878
Recognized Expert Specialist
IGNORE meaning, not insert or insert still but not give error?
-- CK
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
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 -
-
INSERT INTO plis_hist
-
(price_list, part, date_from, volume,dt_created, last_upd_user, price, week_no)
-
(
-
SELECT pls.price_list, pls.part, pls.date_from, pls.volume,
-
pls.dt_created, pls.last_upd_user, pls.price,
-
dbo.FiscalWeek('01',pls.date_from) 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
-
left join plis_hist hist2
-
on (plis.part = hist2.part and plis.price_list = hist2.price_list and plis.volume = hist2.volume and plis.price = hist2.price)
-
where hist2.part is null
-
);
-
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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?
|
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...
|
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...
|
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...
| |
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
|
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
|
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
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |