473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Preventing the message 'Duplicate key was ignored'

My problem is that the 'INSERT INTO' query that sends the records to the
table is dynamically compiled in VBA and and the target table has a two
column primary key. I have made a number of attempts at getting 'WHERE
NOT EXISTS' to cure the problem but so far without success and previous
postings have resulted in advice to create an 'ignore duplicates' index.
This solved the problem in as
much as it allowed the SQL to insert the records that did not already
exist but resulted in the message appearing every time the user ran the
the query. Whilst this is not a major problem it is vaguely irritating
and I would like to find a way to stop it happening. I suspect that the
solution may involve using the @@ERROR command but I am not sure of the
syntax.
Regards
Colin

*** Sent via Developersdex http://www.developersdex.com ***
Jan 10 '06 #1
1 11790
Colin Spalding (pu***@alottole arn.com) writes:
My problem is that the 'INSERT INTO' query that sends the records to the
table is dynamically compiled in VBA and and the target table has a two
column primary key. I have made a number of attempts at getting 'WHERE
NOT EXISTS' to cure the problem but so far without success and previous
postings have resulted in advice to create an 'ignore duplicates' index.
This solved the problem in as
much as it allowed the SQL to insert the records that did not already
exist but resulted in the message appearing every time the user ran the
the query. Whilst this is not a major problem it is vaguely irritating
and I would like to find a way to stop it happening. I suspect that the
solution may involve using the @@ERROR command but I am not sure of the
syntax.


In SQL 2000, there is no possibility to suppress the error. You will
have to this from your client.

In SQL 2005, you can trap the error with new TRY-CATCH syntax.

But the solution with ignore duplicates is really lousy. Instead do
one of:

IF NOT EXISTS (SELECT *
FROM tbl
WHERE pkcol1 = @val1
AND pkcol2 = @val2)
INSERT tbl (pkcol1, pkcol2, ....
VALUES (@val1, @val2, ....)

or

INSERT tbl (pkcol1, pkcol2, ...
SELECT @val1, @val2, ...
WHERE NOT EXISTS (SELECT *
FROM tbl
WHERE pkcol1 = @val1
AND pkcol2 = @val2)

Personally, I prefer the first alternative.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 10 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2342
by: CJM | last post by:
How do people go about preventing the user from submitting a form for a 2nd time? For example, the user submits a form, clicks on the back button, and the submits the form again. I have used various techniques in the past (depending on circumstances) but I'd be interested in the techniques you guys currently use. Thanks --
18
27721
by: Elroyskimms | last post by:
I have a table using an identity column as its Primary Key and two columns (table reduced for simplicity) EmployeeNumber and ArrivalTime. CREATE TABLE ( IDENTITY (1, 1) NOT NULL , (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , NOT NULL , CONSTRAINT PRIMARY KEY CLUSTERED (
3
2180
by: ColinWard | last post by:
I am using the following code to validate that the person that is being entered into the database does not already exist. However wnem I test it by entering myself as a contact(I first checked that I was indeed NOT in the database), the message still comes up saying that I am in the database. what am I doing wrong? Private Sub txtEmailName_AfterUpdate() On Error GoTo Err_txtEmailName_AfterUpdate >> If DLookup("EmailName", "Contacts",...
4
5868
by: FangQ | last post by:
hi I am very new to mysql. I have a question about using the "on duplicate update" clause with insert command. my table "data" has two columns, field1 and field2, where field1 is the index and is "unique". when I run
6
11907
by: Oleg Konovalov | last post by:
Hi, I have a Java/JavaScript GUI application where I perform a lot of long DB operations , which takes 5-60 secs to perform. Sometimes user double-clicks the button or just gets impatient and clicks again, which created duplicate forcm submission and hence duplicate records. So I am trying to disable the button as soon as it is clicked, and as soon as it's done,
8
13104
by: g_man | last post by:
I am trying trap Runtime error 3022 (duplicates) in the click event of a command button that closes the form. I have code in the Form_Error event that does a good job of providing a more meaningful error message than the default. It works in every situation except when the user clicks the close button. I am using Me.Dirty=False to force a save but if there are duplicates I just get the standard Runtime 3022 error message. I am wondering...
0
1588
by: =?Utf-8?B?SGF3aw==?= | last post by:
Hello, I have the need to verifiy a field in a gridview for a duplicate value against a database before the update takes place. I was thinking that the gridview1.rowupdating would be a good place to put code that can check for duplicate data before the update happens. Below is a sample of the code i was trying but did not work. Protected Sub gvAssignedZones_RowUpdating(ByVal sender As Object, ByVal e As...
9
4151
by: rjshrader | last post by:
I have a table (tblStatus) with three fields (CustomerID, StatusType and StatusDate). I use an unbound form with three text boxes to enter data into the table when a command button (cmdSave) is clicked. CustomerID, StatusType are values that are manually entered by the user; StatusDate is automatically filled with the current date that the record is saved. I would like to use code behind the cmdSave button to check the table for...
1
4420
by: traceable1 | last post by:
I have a table with a unique non-clustered index on it. It has the IGNORE_DUP_KEY option on. For some reason, I am getting the following error: ----------------------------------------------------------------------------- System.Data.SqlClient.SqlException: Cannot insert duplicate key row in object 'dbo.table1 with unique index 'IX_table1'. Duplicate key was ignored.
0
9480
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
10324
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
10147
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
8971
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...
0
6739
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
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.