Get ID of last record inserted – VBScript – SQL Server

Here is the technique to get the ID number of the last record inserted (right as you insert it)

Dim db,rcs,new_identity

‘Create a database connection
Set db = Server.CreateObject(“adodb.connection”)
db.Open “DSN=MyDSN”

‘Execute the INSERT statement and the SELECT @@IDENTITY
Set rcs = db.execute(“insert into tablename (fields,..) ” & _
“values (values,…);” & _
“select @@identity”).nextrecordset

‘Retrieve the @@IDENTITY value
new_identity = rcs(0)

Leave a Reply