Classic ASP – Getting ID of new record after insert


Dim loConn, lsSQL, loRs
Set loConn = CreateObject("ADODB.Connection")

' Open a connection to the database
loConn.Open(application("DSN=myDSN;UID=something;PWD=Something;"))

' Insert a new record into the table
lsSQL = "SET NOCOUNT ON;" &_
        "INSERT INTO tbl_test (naziv) VALUES ('pero');" &_
        "SELECT @@IDENTITY AS NewID;"

' Execute the SQL statement
Set loRs = loConn.Execute(lsSQL)

' Get the inserted ID
myNewID= loRs.Fields("NewID").value

response.write myNewID

' Close the connection
loConn.Close()
Set loConn = Nothing

The SET NOCOUNT ON suppresses messages like “1 Row(s) affected” being sent back. These messages are sent back every time SQL statement is executed.

source: http://www.kamath.com/tutorials/tut007_identity.asp


Comments are closed.