{"id":82113,"date":"2011-07-14T01:12:35","date_gmt":"2011-07-14T01:12:35","guid":{"rendered":"https:\/\/www.webstaging.red-gate.com\/simple-talk\/?p=73384"},"modified":"2018-12-12T12:03:31","modified_gmt":"2018-12-12T12:03:31","slug":"denali-enhancement-duplicate-key-error-message","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/denali-enhancement-duplicate-key-error-message\/","title":{"rendered":"Denali Enhancement\u2013Duplicate Key Error Message"},"content":{"rendered":"<p>When I was editing my chapter on implementing a database, I noticed a really nice improvement in the error message I had from the previous edition of the book. Instead of just telling me that there was a value in my modification statement that duplicated an existing value (or multiple values affected by the statement), it told me the duplicated value.<\/p>\n<p>To demo, I created the following quickie table in tempdb.<\/p>\n<p>USE tempdb     <br \/>GO<\/p>\n<p>&#8211;drop the object if it initially existed     <br \/>if object_id(&#8216;test.testErrorMessage&#8217;) IS NOT null      <br \/>&#160;&#160;&#160; DROP TABLE test.testErrorMessage      <br \/>IF schema_id(&#8216;test&#8217;) IS NOT NULL      <br \/>&#160;&#160;&#160; DROP SCHEMA test      <br \/>go<\/p>\n<p>CREATE SCHEMA test     <br \/>GO      <br \/>CREATE TABLE test.testErrorMessage      <br \/>(      <br \/>&#160;&#160;&#160; testErrorMessageId INT NOT NULL      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; CONSTRAINT PKtestErrorMessage PRIMARY KEY,      <br \/>&#160;&#160;&#160; otherColumn varchar(10) NOT NULL      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; CONSTRAINT AKtestErrorMessage UNIQUE (otherColumn),      <br \/>)      <br \/>GO      <br \/>INSERT INTO test.testErrorMessage (testErrorMessageId, otherColumn)      <br \/>VALUES (1,&#8217;First&#8217;)      <br \/>GO<\/p>\n<p>Then, inserting a duplicate row for the primary key value:<\/p>\n<p>INSERT INTO test.testErrorMessage (testErrorMessageId, otherColumn)     <br \/>VALUES (1,&#8217;First&#8217;)      <br \/>GO    <\/p>\n<p>And on 2008 R2, I get:<\/p>\n<p>Msg 2627, Level 14, State 1, Line 1     <br \/>Violation of PRIMARY KEY constraint &#8216;PKtestErrorMessage&#8217;. Cannot insert duplicate key in object &#8216;test.testErrorMessage&#8217;.<\/p>\n<p>Now on Denali CTP3, you get a little bit more:<\/p>\n<p>Msg 2627, Level 14, State 1, Line 1     <br \/>Violation of PRIMARY KEY constraint &#8216;PKtestErrorMessage&#8217;. Cannot insert duplicate key in object &#8216;test.testErrorMessage&#8217;. <strong>The duplicate key value is (1).<\/strong><\/p>\n<p>Then, to show the same thing for the UNIQUE CONSTRAINT:<\/p>\n<p>INSERT INTO test.testErrorMessage (testErrorMessageId,otherColumn)     <br \/>VALUES (2,&#8217;First&#8217;)<\/p>\n<p>On 2008 R2, you get the following<\/p>\n<p>Msg 2627, Level 14, State 1, Line 4     <br \/>Violation of UNIQUE KEY constraint &#8216;AKtestErrorMessage&#8217;. Cannot insert duplicate key in object &#8216;test.testErrorMessage&#8217;.<\/p>\n<p>And again on Denali CTP3:<\/p>\n<p>Msg 2627, Level 14, State 1, Line 4     <br \/>Violation of UNIQUE KEY constraint &#8216;AKtestErrorMessage&#8217;. Cannot insert duplicate key in object &#8216;test.testErrorMessage&#8217;. <strong>The duplicate key value is (First).<\/strong><\/p>\n<p>You can see if you duplicate &gt; 1 value, it gives you one of the items. It might be better if the message didn\u2019t imply that it was the only duplicate value, but hey, it is a great improvement. If you think it ought to be tweaked to say \u201cA duplicated key value is (\u2026) or something, <a href=\"https:\/\/connect.microsoft.com\/SQLServer\/feedback\/details\/679366\/tweak-to-duplicate-key-message\" target=\"_blank\">click here<\/a>) <\/p>\n<p>INSERT INTO test.testErrorMessage (testErrorMessageId,otherColumn)     <br \/>VALUES (5,&#8217;Third&#8217;),(6,&#8217;Third&#8217;),(3,&#8217;Second&#8217;),(4,&#8217;Second&#8217;)<\/p>\n<p>Msg 2627, Level 14, State 1, Line 1     <br \/>Violation of UNIQUE KEY constraint &#8216;AKtestErrorMessage&#8217;. Cannot insert duplicate key in object &#8216;test.testErrorMessage&#8217;. The duplicate key value is (Third).<\/p>\n<p>It works with indexes also:<\/p>\n<p>ALTER TABLE test.testErrorMessage     <br \/>&#160;&#160;&#160;&#160; DROP CONSTRAINT AKtestErrorMessage<\/p>\n<p>CREATE UNIQUE INDEX UXtestErrorMessage ON test.testErrorMessage(otherColumn)<\/p>\n<p>INSERT INTO test.testErrorMessage (testErrorMessageId,otherColumn)     <br \/>VALUES (5,&#8217;Third&#8217;),(6,&#8217;Third&#8217;),(3,&#8217;Second&#8217;),(4,&#8217;Second&#8217;)<\/p>\n<p>You get the following:<\/p>\n<p>Msg 2601, Level 14, State 1, Line 1     <br \/>Cannot insert duplicate key row in object &#8216;test.testErrorMessage&#8217; with unique index &#8216;UXtestErrorMessage&#8217;. The duplicate key value is (Third).    <\/p>\n<p>Much nicer! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I was editing my chapter on implementing a database, I noticed a really nice improvement in the error message I had from the previous edition of the book. Instead of just telling me that there was a value in my modification statement that duplicated an existing value (or multiple values affected by the statement),&#8230;&hellip;<\/p>\n","protected":false},"author":56085,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[19684],"class_list":["post-82113","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/56085"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=82113"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82113\/revisions"}],"predecessor-version":[{"id":82267,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82113\/revisions\/82267"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=82113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=82113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=82113"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=82113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}