<?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: A Database Independent &#8220;Next ID&#8221;</title>
	<atom:link href="http://www.webapper.com/blog/index.php/2007/03/01/a-database-independent-next-id/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webapper.com/blog/index.php/2007/03/01/a-database-independent-next-id/</link>
	<description>Web Application Engineers</description>
	<lastBuildDate>Mon, 05 Dec 2011 17:24:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Clement Huge</title>
		<link>http://www.webapper.com/blog/index.php/2007/03/01/a-database-independent-next-id/comment-page-1/#comment-22839</link>
		<dc:creator>Clement Huge</dc:creator>
		<pubDate>Mon, 28 Jun 2010 19:04:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/A-Database-Independent-Next-ID#comment-22839</guid>
		<description>The major difference between @@identity and Scope_identity() is this one:
SCOPE_IDENTITY() will return the Identity value generated in a table that is currently in scope.
@@IDENTITY will return the Identity value generated in a table irrespective of the scope.
In other words, if you insert a row in 1 table and there is one trigger that inserts a row in another table, the @@identity will give the identity on the second table (that is the last identity) while scope_identity will stay within the scope of the first table.
It is a better practive to use Scope_identity when inserting, updating one row.
If you inserts/updates multiple rows, use the output clause and the Inserted, Deleted &quot;temporary&quot; table within the insert, update operations.

Concerning the multi-threading of Sql server: no matter what will happen within your coldfusion code side, you can only insert, one batch at a time, no matter how many rows you insert.
There is no reason to get the last id you inserted unless you might want to update this very same row.
Inserting new row should be done &quot;blind&quot;.
There is only a few cases I can think of that would require you to know the last ids to insert afterwards. One of them if that this very same id carry some functional aspect like a combo (Date + _ + SomeIncrementalId). 
another reason would be to have several tables that would carry ids to set up some sort of horizontal partitioning on standard version but even so, you can have a job that would check the identity once every day and reseed your tables to make sure we will not get into eachother.</description>
		<content:encoded><![CDATA[<p>The major difference between @@identity and Scope_identity() is this one:<br />
SCOPE_IDENTITY() will return the Identity value generated in a table that is currently in scope.<br />
@@IDENTITY will return the Identity value generated in a table irrespective of the scope.<br />
In other words, if you insert a row in 1 table and there is one trigger that inserts a row in another table, the @@identity will give the identity on the second table (that is the last identity) while scope_identity will stay within the scope of the first table.<br />
It is a better practive to use Scope_identity when inserting, updating one row.<br />
If you inserts/updates multiple rows, use the output clause and the Inserted, Deleted &#8220;temporary&#8221; table within the insert, update operations.</p>
<p>Concerning the multi-threading of Sql server: no matter what will happen within your coldfusion code side, you can only insert, one batch at a time, no matter how many rows you insert.<br />
There is no reason to get the last id you inserted unless you might want to update this very same row.<br />
Inserting new row should be done &#8220;blind&#8221;.<br />
There is only a few cases I can think of that would require you to know the last ids to insert afterwards. One of them if that this very same id carry some functional aspect like a combo (Date + _ + SomeIncrementalId).<br />
another reason would be to have several tables that would carry ids to set up some sort of horizontal partitioning on standard version but even so, you can have a job that would check the identity once every day and reseed your tables to make sure we will not get into eachother.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: franco</title>
		<link>http://www.webapper.com/blog/index.php/2007/03/01/a-database-independent-next-id/comment-page-1/#comment-524</link>
		<dc:creator>franco</dc:creator>
		<pubDate>Sat, 13 Oct 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/A-Database-Independent-Next-ID#comment-524</guid>
		<description>hi daryl

thanks for posting this. i wasn&#039;t aware of the
limitations that the transactional approach
to the issue has.

as a matter of fact, your explanation as to what
exactly happens in terms of locks vis-a-vis isolation levels made me realize that i&#039;d really love to get my hands on a succinct summary of the technical basis behind database transactions.

specifically, i&#039;m looking for a text that explains
isolation levels, locking mechanisms, performance
considerations and all that... basically all the
stuff you seem to know so well.

can you recommend a book, or a website that would help?

thanks,
franco</description>
		<content:encoded><![CDATA[<p>hi daryl</p>
<p>thanks for posting this. i wasn&#8217;t aware of the<br />
limitations that the transactional approach<br />
to the issue has.</p>
<p>as a matter of fact, your explanation as to what<br />
exactly happens in terms of locks vis-a-vis isolation levels made me realize that i&#8217;d really love to get my hands on a succinct summary of the technical basis behind database transactions.</p>
<p>specifically, i&#8217;m looking for a text that explains<br />
isolation levels, locking mechanisms, performance<br />
considerations and all that&#8230; basically all the<br />
stuff you seem to know so well.</p>
<p>can you recommend a book, or a website that would help?</p>
<p>thanks,<br />
franco</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: xiaoxinwow</title>
		<link>http://www.webapper.com/blog/index.php/2007/03/01/a-database-independent-next-id/comment-page-1/#comment-523</link>
		<dc:creator>xiaoxinwow</dc:creator>
		<pubDate>Fri, 06 Apr 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/A-Database-Independent-Next-ID#comment-523</guid>
		<description>Welcome to our website for you World of Warcraft Gold,Wow Gold,Cheap World of Warcraft Gold,cheap wow gold,buy cheap wow gold,real wow gold,sell wow gold, ...
    Here wow gold of 1000 gold at $68.99-$80.99  ,World Of Warcraft Gold,buy wow gold,sell world of warcraft gold(wow gold),buy euro gold wow Cheap wow gold,cheapest wow gold store ... &lt;a href=&quot;http://www.33game.cn&quot;&gt;&lt;strong&gt;ffxi gil&lt;/strong&gt;&lt;/a&gt; buy euro gold wow wow gold--buy cheap wow gold,sell wow gold.welcome to buy cheap wow gold--cheap, easy, wow gold purchasing.World of Warcraft,wow gold Super ...
  We can have your wow gold,buy wow gold,wow gold game,world of warcraft gold, wow Gold Cheap wow, Cheap wow gold,world of warcraft gold deal,Cheap WOW Gold ...
    
  Welcome to our website for you World of Warcraft Gold,Wow Gold,Cheap World of Warcraft Gold,wow gold,buy cheap wow gold,real wow gold,sell wow gold, ...
  Here wow gold of 1000 gold at $68.99-$80.99,World Of Warcraft Gold,buy wow gold,sell world of warcraft gold(wow gold),buy gold wow lightninghoof instock Cheap wow gold,cheapest wow gold store ...
    &lt;strong&gt;ffxi gil&lt;/strong&gt; wow gold--buy cheap wow gold,sell wow gold.welcome to buy cheap wow gold--cheap, easy, wow gold purchasing.World of Warcraft,wow gold Super ...
  Wow gold- Gold for buy gold wow lightninghoof instock EU-Server: ...wow Gold EU: starting from 84,99?; 3000 WoW Gold EU: starting from 119,99?. wow Gold- Leveling Services: ...
  We can have your wow Gold,buy wow Gold,wow Gold game,wow gold, Cheap wow Gold, Cheap World of Warcraft Gold,world of warcraft gold deal,buy cheap wow gold,Cheap WOW Gold ...
    
  Here wow Gold of 1000 gold at $68.99-$80.99,World Of Warcraft Gold,buy wow Gold,sell world of warcraft gold(wow gold),Cheap wow gold,cheapest World of Warcraft Gold store ...</description>
		<content:encoded><![CDATA[<p>Welcome to our website for you World of Warcraft Gold,Wow Gold,Cheap World of Warcraft Gold,cheap wow gold,buy cheap wow gold,real wow gold,sell wow gold, &#8230;<br />
    Here wow gold of 1000 gold at $68.99-$80.99  ,World Of Warcraft Gold,buy wow gold,sell world of warcraft gold(wow gold),buy euro gold wow Cheap wow gold,cheapest wow gold store &#8230; &lt;a href=&quot;http://www.33game.cn&quot;&gt;&lt;strong&gt;ffxi gil&lt;/strong&gt;&lt;/a&gt; buy euro gold wow wow gold&#8211;buy cheap wow gold,sell wow gold.welcome to buy cheap wow gold&#8211;cheap, easy, wow gold purchasing.World of Warcraft,wow gold Super &#8230;<br />
  We can have your wow gold,buy wow gold,wow gold game,world of warcraft gold, wow Gold Cheap wow, Cheap wow gold,world of warcraft gold deal,Cheap WOW Gold &#8230;</p>
<p>  Welcome to our website for you World of Warcraft Gold,Wow Gold,Cheap World of Warcraft Gold,wow gold,buy cheap wow gold,real wow gold,sell wow gold, &#8230;<br />
  Here wow gold of 1000 gold at $68.99-$80.99,World Of Warcraft Gold,buy wow gold,sell world of warcraft gold(wow gold),buy gold wow lightninghoof instock Cheap wow gold,cheapest wow gold store &#8230;<br />
    &lt;strong&gt;ffxi gil&lt;/strong&gt; wow gold&#8211;buy cheap wow gold,sell wow gold.welcome to buy cheap wow gold&#8211;cheap, easy, wow gold purchasing.World of Warcraft,wow gold Super &#8230;<br />
  Wow gold- Gold for buy gold wow lightninghoof instock EU-Server: &#8230;wow Gold EU: starting from 84,99?; 3000 WoW Gold EU: starting from 119,99?. wow Gold- Leveling Services: &#8230;<br />
  We can have your wow Gold,buy wow Gold,wow Gold game,wow gold, Cheap wow Gold, Cheap World of Warcraft Gold,world of warcraft gold deal,buy cheap wow gold,Cheap WOW Gold &#8230;</p>
<p>  Here wow Gold of 1000 gold at $68.99-$80.99,World Of Warcraft Gold,buy wow Gold,sell world of warcraft gold(wow gold),Cheap wow gold,cheapest World of Warcraft Gold store &#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nat Papovich</title>
		<link>http://www.webapper.com/blog/index.php/2007/03/01/a-database-independent-next-id/comment-page-1/#comment-514</link>
		<dc:creator>Nat Papovich</dc:creator>
		<pubDate>Thu, 01 Mar 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/A-Database-Independent-Next-ID#comment-514</guid>
		<description>Hi Daryl, I know this is a cross-database post, but is the following okay for SQL Server only?

set nocount on
insert into ...
select @@identity as newid
set nocount off

I&#039;ve been using that for years on SQL Server...</description>
		<content:encoded><![CDATA[<p>Hi Daryl, I know this is a cross-database post, but is the following okay for SQL Server only?</p>
<p>set nocount on<br />
insert into &#8230;<br />
select @@identity as newid<br />
set nocount off</p>
<p>I&#8217;ve been using that for years on SQL Server&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Wolf</title>
		<link>http://www.webapper.com/blog/index.php/2007/03/01/a-database-independent-next-id/comment-page-1/#comment-515</link>
		<dc:creator>Dave Wolf</dc:creator>
		<pubDate>Thu, 01 Mar 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/A-Database-Independent-Next-ID#comment-515</guid>
		<description>Do note that this approach will single thread your entire server architecture for insert operations due to lock contention on the row/page of the single row key table.  Anytime you issue the update, then select to get the next value, the RDBMS will lock the single row/page of the key table until the end of the transaction.  The UPDATE lock is an exclusive lock which will prevent the next key generation call from being able to do its own UPDATE operation.  This means you can only generate a single key within a single logical unit of work (i.e. critical section).  Although this is indeed thread-safe it is also completely serialized.  In other words, if you had an order entry system where 1,000 orders all occurred at the same time, they would all require generation of a primary key value and hence would not occur in parallel but in a purely serial FIFO fashion.  There are several table based solutions folks have used including pre-allocating blocks keys into a keystaging table.  With row-level locking by having a block of keys in the staging table and not bump into contention issues on the read/delete of the next available key.  

Celko also has several great solutions for this issue.</description>
		<content:encoded><![CDATA[<p>Do note that this approach will single thread your entire server architecture for insert operations due to lock contention on the row/page of the single row key table.  Anytime you issue the update, then select to get the next value, the RDBMS will lock the single row/page of the key table until the end of the transaction.  The UPDATE lock is an exclusive lock which will prevent the next key generation call from being able to do its own UPDATE operation.  This means you can only generate a single key within a single logical unit of work (i.e. critical section).  Although this is indeed thread-safe it is also completely serialized.  In other words, if you had an order entry system where 1,000 orders all occurred at the same time, they would all require generation of a primary key value and hence would not occur in parallel but in a purely serial FIFO fashion.  There are several table based solutions folks have used including pre-allocating blocks keys into a keystaging table.  With row-level locking by having a block of keys in the staging table and not bump into contention issues on the read/delete of the next available key.  </p>
<p>Celko also has several great solutions for this issue.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Wolf</title>
		<link>http://www.webapper.com/blog/index.php/2007/03/01/a-database-independent-next-id/comment-page-1/#comment-516</link>
		<dc:creator>Dave Wolf</dc:creator>
		<pubDate>Thu, 01 Mar 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/A-Database-Independent-Next-ID#comment-516</guid>
		<description>The @@identity trick in SQLServer is also very dubious and can return very erroneous results.  Basically @@identity is a global variable, and returns back the last identity value generated.  So take the case where you insert a row, and a trigger fires to populate a child table, or other metadata and that table itself has an identity value.  @@identity returns the second identity value, the one caused by the insert from the trigger and not from the parent table.  Since the child table could have less rows, it could very easily return a value for an already used key.  @@identity is very much not thread safe.</description>
		<content:encoded><![CDATA[<p>The @@identity trick in SQLServer is also very dubious and can return very erroneous results.  Basically @@identity is a global variable, and returns back the last identity value generated.  So take the case where you insert a row, and a trigger fires to populate a child table, or other metadata and that table itself has an identity value.  @@identity returns the second identity value, the one caused by the insert from the trigger and not from the parent table.  Since the child table could have less rows, it could very easily return a value for an already used key.  @@identity is very much not thread safe.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam</title>
		<link>http://www.webapper.com/blog/index.php/2007/03/01/a-database-independent-next-id/comment-page-1/#comment-517</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Thu, 01 Mar 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/A-Database-Independent-Next-ID#comment-517</guid>
		<description>&quot;Wrapping them in a transaction doesn&#039;t help. &quot;

Would you mind going into more depth on that?  I always thought when the docs said cftransaction &quot;groups database queries into a unit,&quot; it pretty much meant that would be the case.</description>
		<content:encoded><![CDATA[<p>&quot;Wrapping them in a transaction doesn&#8217;t help. &quot;</p>
<p>Would you mind going into more depth on that?  I always thought when the docs said cftransaction &quot;groups database queries into a unit,&quot; it pretty much meant that would be the case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daryl Banttari</title>
		<link>http://www.webapper.com/blog/index.php/2007/03/01/a-database-independent-next-id/comment-page-1/#comment-518</link>
		<dc:creator>Daryl Banttari</dc:creator>
		<pubDate>Thu, 01 Mar 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/A-Database-Independent-Next-ID#comment-518</guid>
		<description>Nat,

Using @@Identity is fine for SQL Server, so long as you don&#039;t have triggers that do inserts into other tables that have Identity columns.

Dave,

Any time you have any variable that you want to update consistently across threads, you MUST single-thread changes to the variable.  To do so, you MUST have some sort of semaphore to control access across threads; in this case, it&#039;s the update lock.  The transaction block where you&#039;re single threading is blindingly short, and should be complete in 4ms or less, so concurrency really only becomes a problem if you&#039;re doing 250+ inserts a second, in which case using something DBMS-specific is probably a good idea.

In SQL Server, @@Identity is not a &quot;trick&quot;, it&#039;s a well defined feature, and will only give &quot;dubious&quot; or &quot;very erroneous&quot; results if you either (a) have triggers, or (b) don&#039;t do the SELECT @@IDENTITY operation before the next identity insert.  One popular mistake is to do &quot;SELECT @@IDENTITY FROM SomeTable&quot;, which simply does a Cartesian join from the constant returned by @@IDENTITY, and the rows in SomeTable.  So, if SomeTable had 100,000 rows in this case, you&#039;d get 100,000 copies of the @@IDENTITY value.  And if your last identity insert was actually for SomeOtherTable, you&#039;d get a completely unexpected value.

Also, @@IDENTITY is not a global variable, it&#039;s a connection variable.  So it doesn&#039;t return the last  identity value inserted, it returns the last identity value inserted /on the current connection/.  Since ColdFusion pools connections on a per-page basis even if you have connection pooling turned off, it&#039;s completely safe from that perspective.

If you have triggers, the scope_identity() function is designed to return the proper identity value, like this:

&lt;code&gt;
&lt;cfquery name=&quot;q&quot; datasource=&quot;Northwind&quot;&gt;
	INSERT INTO SomeTable
	VALUES (&#039;foo&#039;)
&lt;/cfquery&gt;
&lt;cfquery name=&quot;q&quot; datasource=&quot;Northwind&quot;&gt;
	SELECT SCOPE_IDENTITY() AS ID
&lt;/cfquery&gt;
&lt;/code&gt;

Sam,

A transaction groups queries into a logical group of work, and ensures that all of the *changes* in the transaction either commit, or roll back.  However, a transaction doesn&#039;t change the isolation level for reads, which by default is &quot;READ COMMITTED&quot;.  READ COMMITTED will acquire and hold read locks only long enough to read the row(s), and release the lock once the row is read.  The next level, REPEATABLE READ will hold locks on any rows that were actually read until the end of the transaction, and SERIALIZABLE will go so far as to prevent inserts between ranges you selected from.  But none of these prevent you from reading a value that another thread/connection has read (since read locks can be made concurrently), so the sort of read consistency you&#039;re expecting is never enforced.  This is why I do the update first, as that will actually block other threads/connections from reading the value I updated until the end of the transaction.  (The isolation level READ COMMITTED won&#039;t read the row until the transaction is committed.)

Hope that helps... :-)

--Daryl</description>
		<content:encoded><![CDATA[<p>Nat,</p>
<p>Using @@Identity is fine for SQL Server, so long as you don&#8217;t have triggers that do inserts into other tables that have Identity columns.</p>
<p>Dave,</p>
<p>Any time you have any variable that you want to update consistently across threads, you MUST single-thread changes to the variable.  To do so, you MUST have some sort of semaphore to control access across threads; in this case, it&#8217;s the update lock.  The transaction block where you&#8217;re single threading is blindingly short, and should be complete in 4ms or less, so concurrency really only becomes a problem if you&#8217;re doing 250+ inserts a second, in which case using something DBMS-specific is probably a good idea.</p>
<p>In SQL Server, @@Identity is not a &quot;trick&quot;, it&#8217;s a well defined feature, and will only give &quot;dubious&quot; or &quot;very erroneous&quot; results if you either (a) have triggers, or (b) don&#8217;t do the SELECT @@IDENTITY operation before the next identity insert.  One popular mistake is to do &quot;SELECT @@IDENTITY FROM SomeTable&quot;, which simply does a Cartesian join from the constant returned by @@IDENTITY, and the rows in SomeTable.  So, if SomeTable had 100,000 rows in this case, you&#8217;d get 100,000 copies of the @@IDENTITY value.  And if your last identity insert was actually for SomeOtherTable, you&#8217;d get a completely unexpected value.</p>
<p>Also, @@IDENTITY is not a global variable, it&#8217;s a connection variable.  So it doesn&#8217;t return the last  identity value inserted, it returns the last identity value inserted /on the current connection/.  Since ColdFusion pools connections on a per-page basis even if you have connection pooling turned off, it&#8217;s completely safe from that perspective.</p>
<p>If you have triggers, the scope_identity() function is designed to return the proper identity value, like this:</p>
<p>&lt;code&gt;<br />
&lt;cfquery name=&quot;q&quot; datasource=&quot;Northwind&quot;&gt;<br />
	INSERT INTO SomeTable<br />
	VALUES (&#8216;foo&#8217;)<br />
&lt;/cfquery&gt;<br />
&lt;cfquery name=&quot;q&quot; datasource=&quot;Northwind&quot;&gt;<br />
	SELECT SCOPE_IDENTITY() AS ID<br />
&lt;/cfquery&gt;<br />
&lt;/code&gt;</p>
<p>Sam,</p>
<p>A transaction groups queries into a logical group of work, and ensures that all of the *changes* in the transaction either commit, or roll back.  However, a transaction doesn&#8217;t change the isolation level for reads, which by default is &quot;READ COMMITTED&quot;.  READ COMMITTED will acquire and hold read locks only long enough to read the row(s), and release the lock once the row is read.  The next level, REPEATABLE READ will hold locks on any rows that were actually read until the end of the transaction, and SERIALIZABLE will go so far as to prevent inserts between ranges you selected from.  But none of these prevent you from reading a value that another thread/connection has read (since read locks can be made concurrently), so the sort of read consistency you&#8217;re expecting is never enforced.  This is why I do the update first, as that will actually block other threads/connections from reading the value I updated until the end of the transaction.  (The isolation level READ COMMITTED won&#8217;t read the row until the transaction is committed.)</p>
<p>Hope that helps&#8230; <img src='http://www.webapper.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>&#8211;Daryl</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam</title>
		<link>http://www.webapper.com/blog/index.php/2007/03/01/a-database-independent-next-id/comment-page-1/#comment-519</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Thu, 01 Mar 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/A-Database-Independent-Next-ID#comment-519</guid>
		<description>Thanks, that did explain it quite well.</description>
		<content:encoded><![CDATA[<p>Thanks, that did explain it quite well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott Krebs</title>
		<link>http://www.webapper.com/blog/index.php/2007/03/01/a-database-independent-next-id/comment-page-1/#comment-520</link>
		<dc:creator>Scott Krebs</dc:creator>
		<pubDate>Thu, 01 Mar 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/A-Database-Independent-Next-ID#comment-520</guid>
		<description>Interesting timing on this post, Daryl. I discovered something yesterday that seems to have changed in CF although I&#039;m not sure when. I&#039;m developing an app on 7.0.2 w/ MSSQL and discovered that I could do the insert and select scope_identity() as newID within the same cfquery tag set and it works just fine. I remember having to use nocount on/off in the past (as Nat indicated) to pull this off. I&#039;m not sure when this changed, just that it works now without it and in the same cfquery tag.

So:
[cfquery name=&quot;insQuery&quot;]
INSERT INTO SomeTable
(stuff, things)
VALUES
(&quot;stuff&quot;,&quot;things&quot;)
SELECT SCOPE_IDENTITY() AS newID
[/cfquery]
will leave you with #insQuery.newID# as your new id.

While it seems to work just fine, I&#039;m also not sure if there&#039;s a good reason to *not* do it that way so if anyone knows that to be a bad idea for some reason please enlighten. It&#039;s all done within the context of a transaction with isolation=&quot;SERIALIZABLE&quot; (as the queryname.newID is used to populate child tables and such and performance/scalability isn&#039;t an issue in this case) so it all seems pretty safe.</description>
		<content:encoded><![CDATA[<p>Interesting timing on this post, Daryl. I discovered something yesterday that seems to have changed in CF although I&#8217;m not sure when. I&#8217;m developing an app on 7.0.2 w/ MSSQL and discovered that I could do the insert and select scope_identity() as newID within the same cfquery tag set and it works just fine. I remember having to use nocount on/off in the past (as Nat indicated) to pull this off. I&#8217;m not sure when this changed, just that it works now without it and in the same cfquery tag.</p>
<p>So:<br />
[cfquery name=&quot;insQuery&quot;]<br />
INSERT INTO SomeTable<br />
(stuff, things)<br />
VALUES<br />
(&quot;stuff&quot;,&quot;things&quot;)<br />
SELECT SCOPE_IDENTITY() AS newID<br />
[/cfquery]<br />
will leave you with #insQuery.newID# as your new id.</p>
<p>While it seems to work just fine, I&#8217;m also not sure if there&#8217;s a good reason to *not* do it that way so if anyone knows that to be a bad idea for some reason please enlighten. It&#8217;s all done within the context of a transaction with isolation=&quot;SERIALIZABLE&quot; (as the queryname.newID is used to populate child tables and such and performance/scalability isn&#8217;t an issue in this case) so it all seems pretty safe.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

