<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'T-SQL', 'Constraints', and 'Denali'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=T-SQL,Constraints,Denali&amp;orTags=0</link><description>Search results matching tags 'T-SQL', 'Constraints', and 'Denali'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Denali Enhancement–Duplicate Key Error Message</title><link>http://sqlblog.com/blogs/louis_davidson/archive/2011/07/14/denali-enhancement-duplicate-key-error-message.aspx</link><pubDate>Thu, 14 Jul 2011 05:12:35 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:36921</guid><dc:creator>drsql</dc:creator><description>&lt;p&gt;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.&lt;/p&gt;  &lt;p&gt;To demo, I created the following quickie table in tempdb.&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;USE tempdb     &lt;br /&gt;GO&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;--drop the object if it initially existed     &lt;br /&gt;if object_id('test.testErrorMessage') IS NOT null      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; DROP TABLE test.testErrorMessage      &lt;br /&gt;IF schema_id('test') IS NOT NULL      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; DROP SCHEMA test      &lt;br /&gt;go&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;CREATE SCHEMA test     &lt;br /&gt;GO      &lt;br /&gt;CREATE TABLE test.testErrorMessage      &lt;br /&gt;(      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; testErrorMessageId INT NOT NULL      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CONSTRAINT PKtestErrorMessage PRIMARY KEY,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; otherColumn varchar(10) NOT NULL      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CONSTRAINT AKtestErrorMessage UNIQUE (otherColumn),      &lt;br /&gt;)      &lt;br /&gt;GO      &lt;br /&gt;INSERT INTO test.testErrorMessage (testErrorMessageId, otherColumn)      &lt;br /&gt;VALUES (1,'First')      &lt;br /&gt;GO&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Then, inserting a duplicate row for the primary key value:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;INSERT INTO test.testErrorMessage (testErrorMessageId, otherColumn)     &lt;br /&gt;VALUES (1,'First')      &lt;br /&gt;GO&lt;/font&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;And on 2008 R2, I get:&lt;/p&gt;  &lt;p&gt;&lt;font color="#c0504d"&gt;Msg 2627, Level 14, State 1, Line 1     &lt;br /&gt;Violation of PRIMARY KEY constraint 'PKtestErrorMessage'. Cannot insert duplicate key in object 'test.testErrorMessage'.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Now on Denali CTP3, you get a little bit more:&lt;/p&gt;  &lt;p&gt;&lt;font color="#c0504d"&gt;Msg 2627, Level 14, State 1, Line 1     &lt;br /&gt;Violation of PRIMARY KEY constraint 'PKtestErrorMessage'. Cannot insert duplicate key in object 'test.testErrorMessage'. &lt;strong&gt;The duplicate key value is (1).&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Then, to show the same thing for the UNIQUE CONSTRAINT:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;INSERT INTO test.testErrorMessage (testErrorMessageId,otherColumn)     &lt;br /&gt;VALUES (2,'First')&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;On 2008 R2, you get the following&lt;/p&gt;  &lt;p&gt;&lt;font color="#c0504d"&gt;Msg 2627, Level 14, State 1, Line 4     &lt;br /&gt;Violation of UNIQUE KEY constraint 'AKtestErrorMessage'. Cannot insert duplicate key in object 'test.testErrorMessage'.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;And again on Denali CTP3:&lt;/p&gt;  &lt;p&gt;&lt;font color="#c0504d"&gt;Msg 2627, Level 14, State 1, Line 4     &lt;br /&gt;Violation of UNIQUE KEY constraint 'AKtestErrorMessage'. Cannot insert duplicate key in object 'test.testErrorMessage'. &lt;strong&gt;The duplicate key value is (First).&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;You can see if you duplicate &amp;gt; 1 value, it gives you one of the items. It might be better if the message didn’t 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 “A duplicated key value is (…) or something, &lt;a href="https://connect.microsoft.com/SQLServer/feedback/details/679366/tweak-to-duplicate-key-message" target="_blank"&gt;click here&lt;/a&gt;) &lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;INSERT INTO test.testErrorMessage (testErrorMessageId,otherColumn)     &lt;br /&gt;VALUES (5,'Third'),(6,'Third'),(3,'Second'),(4,'Second')&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#c0504d"&gt;Msg 2627, Level 14, State 1, Line 1     &lt;br /&gt;Violation of UNIQUE KEY constraint 'AKtestErrorMessage'. Cannot insert duplicate key in object 'test.testErrorMessage'. The duplicate key value is (Third).&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;It works with indexes also:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;ALTER TABLE test.testErrorMessage     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DROP CONSTRAINT AKtestErrorMessage&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;CREATE UNIQUE INDEX UXtestErrorMessage ON test.testErrorMessage(otherColumn)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;INSERT INTO test.testErrorMessage (testErrorMessageId,otherColumn)     &lt;br /&gt;VALUES (5,'Third'),(6,'Third'),(3,'Second'),(4,'Second')&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;You get the following:&lt;/p&gt;  &lt;p&gt;&lt;font color="#c0504d"&gt;Msg 2601, Level 14, State 1, Line 1     &lt;br /&gt;Cannot insert duplicate key row in object 'test.testErrorMessage' with unique index 'UXtestErrorMessage'. The duplicate key value is (Third).&lt;/font&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;Much nicer! &lt;/p&gt;</description></item></channel></rss>