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 2008 Forums
 Transact-SQL (2008)
 Retrieve Identity after insert

Author  Topic 

kdeutsch
Yak Posting Veteran

83 Posts

Posted - 2010-12-01 : 12:37:13
I am trying to get the identiy number after I do an insert into a table. From everything I have found it goes like this

sql = "Insert tblSRPType (strSRPType, dtCreated, strCreatedby, bitActive) VALUES ('" & SRP & "', '" & Date.Today & "', '" & Userlogon & "', 1); " _
& "Select scope_identity()"

insertUpdateDelete(sql)

SRpId = getData(sql).Rows(0)(0)

lblTest.Text = SRpId

It does work but it doe a double insert into my database each time, but if I take out the portion to get identiy it only does a single insert. This code worked fine in dot.net 1.1 and sql 2005, but we are upgrading to dot.net 4.0 and sql 2008 and this does not seem to work.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-12-01 : 13:38:34
better to wrap this in a procedure and return retrieved id from it. then call the procedure from your app

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

stonebreaker
Yak Posting Veteran

92 Posts

Posted - 2010-12-14 : 09:09:19
+1 on putting it in a stored proc. Then return SCOPE_IDENTITY().

Stonebreaker
The greatest obstacle to discovery is not ignorance - it is the illusion of knowledge.
-Daniel Boorstin
Go to Top of Page
   

- Advertisement -