Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ignore dup key warning

16 views
Skip to first unread message

Andre

unread,
Dec 28, 2009, 1:15:28 PM12/28/09
to
Is it possible to suppress the warning that is generated when you have an
index with IGNORE_DUP_KEY and you try to insert duplicate records? The
warning is causing our jobs, such as DTS, to fail because it's not a
success code.

Thanks, Andre

Plamen Ratchev

unread,
Dec 28, 2009, 1:41:06 PM12/28/09
to
If you are using SQL Server 2000 then I do not think there is a way to suppress the warning. On SQL Server 2005/2008 you
can use TRY/CATCH to trap it.

However, the real solution is to avoid using IGNORE_DUP_KEY and to properly code your SQL code to avoid duplicates (for
example, using a predicate with NOT EXISTS to insert only keys that do not exist in the table).

--
Plamen Ratchev
http://www.SQLStudio.com

Andre

unread,
Dec 28, 2009, 1:50:17 PM12/28/09
to
Thanks for the response. We'll look at try/catch as we're in SQL 2008.

Andre

Erland Sommarskog

unread,
Dec 28, 2009, 5:53:53 PM12/28/09
to
Plamen Ratchev (Pla...@SQLStudio.com) writes:
> If you are using SQL Server 2000 then I do not think there is a way to
> suppress the warning. On SQL Server 2005/2008 you can use TRY/CATCH to
> trap it.

I don't think you can, because it is only a warning.

But on SQL 2000 the message is raised with level 16 - without setting
@@error.

Andr� mentioned DTS, maybe DTS thinks it's an error too. In that
case, more orthodox coding is to recommend. (Personally, I don't
use IGNORE_DUP_KEY very often, but I did just it in a UDF that I
posted recently, because I needed to make sure that the UDF returns
unique values, but I knew the duplicates would only be occasional.)

--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Plamen Ratchev

unread,
Dec 28, 2009, 9:41:49 PM12/28/09
to
You are correct Erland. I did not realize it was only a warning and TRY/CATCH cannot trap it.

I have never used IGNORE_DUP_KEY in production code. Much better to code the SQL statements correct.

0 new messages