Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Identity Primary Key

Author  Topic 

Sep410
Posting Yak Master

117 Posts

Posted - 2008-08-19 : 17:09:02
Hi all,
I am working an application in vb.net.Users can insert new record to a tables. Primary key to that table is a Identity field.
Is there any way I can get this Id as a return value of my insert?


Sep

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-08-19 : 17:11:22
DECLARE @i int

INSERT ...

SET @i = SCOPE_IDENTITY() --avoid @@IDENTITY for trigger reasons

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Sep410
Posting Yak Master

117 Posts

Posted - 2008-08-19 : 17:38:39
Thanks Tara.

Sep
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-20 : 00:12:41
Or you could even use IDENT_CURRENT('YourTableName') to get id generated for table explicitly.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-20 : 00:15:07
refer below to understand the difference between them

http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/
Go to Top of Page
   

- Advertisement -