<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: New ID for record in SQL Server</title>
	<atom:link href="http://colrosa.co.uk/blog/new-id-for-record-in-sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://colrosa.co.uk/blog/new-id-for-record-in-sql-server/</link>
	<description>Business Focused Technical Advice</description>
	<pubDate>Tue, 06 Jan 2009 20:50:44 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
	<item>
		<title>By: Miles Dennis</title>
		<link>http://colrosa.co.uk/blog/new-id-for-record-in-sql-server/comment-page-1/#comment-105</link>
		<dc:creator>Miles Dennis</dc:creator>
		<pubDate>Tue, 15 Apr 2008 15:21:44 +0000</pubDate>
		<guid isPermaLink="false">http://colrosa.co.uk/blog/new-id-for-record-in-sql-server/#comment-105</guid>
		<description>The reason the two queries sometimes return different values is due to what happens when an insert fails.

Suppose your table has two records in it, with Id's 1 and 2 and no attempt has been made to add a 3rd record. In this case the two queries above will return the same values i.e. 2.  Now let's insert another record, but we'll effectively cause it to fail (by starting and rolling back a transaction).

BEGIN TRANSACTION
INSERT INTO mytables VALUES ('xxx')  /* not sure what your fields are */
ROLLBACK TRANSACTION

Now, SELECT MAX(id) will return 2 as before, but IDENT_CURRENT will return 3 as this was the last attempted value.

If you now do an successful INSERT, both methods will return 4.

As you can see, it's vital that you perform all operations including the selection of the new id in a transaction.

From a performance point of view, the IDENT_CURRENT should generally be faster.

Miles</description>
		<content:encoded><![CDATA[<p>The reason the two queries sometimes return different values is due to what happens when an insert fails.</p>
<p>Suppose your table has two records in it, with Id&#8217;s 1 and 2 and no attempt has been made to add a 3rd record. In this case the two queries above will return the same values i.e. 2.  Now let&#8217;s insert another record, but we&#8217;ll effectively cause it to fail (by starting and rolling back a transaction).</p>
<p>BEGIN TRANSACTION<br />
INSERT INTO mytables VALUES (&#8217;xxx&#8217;)  /* not sure what your fields are */<br />
ROLLBACK TRANSACTION</p>
<p>Now, SELECT MAX(id) will return 2 as before, but IDENT_CURRENT will return 3 as this was the last attempted value.</p>
<p>If you now do an successful INSERT, both methods will return 4.</p>
<p>As you can see, it&#8217;s vital that you perform all operations including the selection of the new id in a transaction.</p>
<p>From a performance point of view, the IDENT_CURRENT should generally be faster.</p>
<p>Miles</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Thomas</title>
		<link>http://colrosa.co.uk/blog/new-id-for-record-in-sql-server/comment-page-1/#comment-104</link>
		<dc:creator>Daniel Thomas</dc:creator>
		<pubDate>Tue, 15 Apr 2008 09:16:21 +0000</pubDate>
		<guid isPermaLink="false">http://colrosa.co.uk/blog/new-id-for-record-in-sql-server/#comment-104</guid>
		<description>Connected with the above post, when I do the following I sometimes get different results - why?

SELECT IDENT_CURRENT('my_table')
SELECT MAX(id) FROM my_table

also, which is more efficient?

Cheers
Dan</description>
		<content:encoded><![CDATA[<p>Connected with the above post, when I do the following I sometimes get different results - why?</p>
<p>SELECT IDENT_CURRENT(&#8217;my_table&#8217;)<br />
SELECT MAX(id) FROM my_table</p>
<p>also, which is more efficient?</p>
<p>Cheers<br />
Dan</p>
]]></content:encoded>
	</item>
</channel>
</rss>
