Results 1 to 3 of 3

Thread: Grabbing identity key from INSERT command

  1. #1

    Thread Starter
    Addicted Member Halon's Avatar
    Join Date
    Oct 2002
    Location
    under desk choking on rage
    Posts
    228

    Talking Grabbing identity key from INSERT command

    Hey people,

    I am looking for tips or suggestions for a speedy way to Insert a record and then return the unique id that was created (the PK autoincrement)

    Right now i do it like so
    Code:
    sSQLReg = "INSERT INTO SWRegister values (" & iclubnum & ",'" & ifirstname & "','n/a','" & ilastname & "')" 'new or existing user 1 new 0 existing
    	Set objRSReg = Server.CreateObject("ADODB.RecordSet")
    	objRSReg.Open sSQLReg,objConn
    
    	sSQLID = "SELECT UserID FROm SWRegister where ClubNumber =" & iclubnum & " and FirstName=" & ifirstname & ")" 
    	Set objRSID = Server.CreateObject("ADODB.RecordSet")
    	objRSID.Open sSQLID,objConn
    	idnum = objRSID("UserID")
    Inserting thedata and then doing a select statement to grab the key i just created.
    Thing is I know there is a better way, as I do it in .NET using stored procedures that return an @@identity to a datareader etc.

    I don't have access to the DB to use a stored procedure so stuck with calls within asp.

    It works fine as is but once the app goes live there could be 100's of users trying to insert at once (hopefully) and i think my method may cause a bottleneck or errors

    Thanks in advance everyone!!
    Soylent Green tastes like chicken

  2. #2
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    You may need use an AddNew instead. It's easy way to do it.
    Code:
    Dim rs, conn
    
    conn=Your_Connection_String
    
    set rs=server.createobject("ADODB.Recordset")
    rs.cursorlocation=3
    rs.open "SWRegister", conn, 3, 3
    
    rs.AddNew
    idnum=rs("UserID")
    rs("FieldName1")=Variable1
    ...
    ...
    ...
    rs.Update
    rs.close
    set rs=nothing

  3. #3
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310
    Here is a small snippet of using @@IDENTITY without a stored proc.

    VB Code:
    1. sSQL = "SET NOCOUNT ON INSERT INTO  tblAuthors (EntryDate) VALUES ('" & Date & "') SELECT @@IDENTITY"
    2. rsTemp.Open sSQL, cn, adOpenStatic, adLockReadOnly
    3. mlngRSKey = rsTemp.Fields(0)
    4. mlngAu_ID = mlngRSKey
    5. rsTemp.Close

    What we did (not the most efficiant but worked flawlessly) was to open the connection, add an empty record and get the key, do an update to populate the rest of the record then close the connection. I no longer remember what the project spec was that required that type of situation anymore. In the end the code worked very well. The key was the SET NOCOUNT ON. We could have added the entire record in the update and not done an update. The end result would have been the same.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width