Home   Free Applications   Code Snippets   Fun Stuff 
 
 You are here: Home > Code Snippets > SQL
Register   Login  

 
Site Navigation


Print Print this page
Email E-mail this page
Bookmark Add to Favorites

 
Return Idenity on Insert

Posted by on Friday, October 17, 2003 (EST)

The following sql will return the Identity value after an insert

This will return a record set with one row and a column of ID with the value of the id that was inserted.

INSERT INTO TableName (Field1, Field2, FieldN)  
VALUES ('Value1', 'Value2', 'ValueN')
SELECT @@IDENTITY as ID

Comments:

Casting identity
By ? on Thursday, October 23, 2003 (EST)

Don't you have to cast the identity to int ?

Select cast( @@identity as int) as ID

btw, cool site. What portal system are you using ?

Reply to this Comment

Warning
By ? on Monday, November 10, 2003 (EST)
This works well (and I've been using this for ages) unless you're using triggers. Have a look at this: http://weblogs.sqlteam.com/travisl/posts/405.aspx

Reply to this Comment

If using SQL 2000
By ? on Wednesday, October 06, 2004 (EST)

If youa re using SQL 2000 you are going to want to use the new SCOPE_IDENTITY() function. The reason being is that @@IDENTITY will retun you the last IDENTITY value inserted into a table. This means that if you have triggers that run on insert and put rows into other tables, the identity value that you are going to get will be from that table, not the one you intended.

SELECT SCOPE_IDENTITY() as ID

Reply to this Comment

qweasdqweas
By ? on Monday, March 28, 2005 (EST)
asdasdqweasdweasdsdf dfsdfqwasd asd awedasdasd asdqwas adasdqwe asd sdasd adawdsdasd ascasd

Reply to this Comment

qqqqq
By ? on Monday, March 28, 2005 (EST)

Reply to this Comment

Add Your Comment



Top
 
CNET News.com


Tech news and business reports by CNET News.com. Focused on information technology, core topics include computers, hardware, software, networking, and Internet media.

  • Microsoft adding to its Labs collection
    Startup Labs will be an effort under Ray Ozzie, joining other efforts including Live Labs, adCenter Labs and Office Labs.

  • Security Bites 113: The security of Chrome
    Billy Hoffman of HP Web Security talks about Google's new browser, how it handles Javascript and what it means for Web 2.0 security.

  • Are Demo and TechCrunch50 fragmenting their audiences?
    With both events scheduled to start Monday, many press, as well as venture capitalists and others are having to choose which one to attend.

  • A user guide to following DemoFall and TechCrunch50
    More than 100 products are going to be introduced during the week of September 8. Will they be any good?

  • What's McCain doing in front of my junior high?
    Speculation has it that it was a goof-up that had the Republican nominee standing in front of Walter Reed Middle School during his acceptance speech. In any case, the image brings back memories for CNET News' Ina Fried.


    Copyright 1995-2008 CNET Networks, Inc. All rights reserved. Last Refreshed 9/5/2008 3:33:04 PM
  •