Insert into db

My problem is this. I have an insert statement and I create a sqlcommand object and executeScalar() to run the statement.

Now I have to take the identity of that inserted row and put it into another table. How can I get back the identity off the newly inserted row

 

Thanks



Answer this question

Insert into db

  • Jim Wooley

    if you are using stored procedure, u have to store the value of the identity into a parameter. For example:

    @MyID int OUTPUT

    Insert..........

    @MyID = @@Identity

    in your code, to get the value of the identity:

    cmd.parameters("@MyID").value



  • Hercules

    Dear Customer,

    I am just checking to see how this is going.
    Did Spotty's suggestion help you
    Please drop me a quick note at your convenience to let me know the current status of this issue.
    If you have any concerns, please do not hesitate to let me know.
    Thanks, and have a great day! :)

    Best regards,
    Peter Huang



  • nirbhay194133

    I think what you may be looking for is the @@identity

    http://www.sqlteam.com/item.asp ItemID=8003

    So when you insert a record into the database - you can get the newly created Identity to use in another SQL statement.

    You can use this and wrap it up in a SQL stored procedure which enables you to execute a number of SQL statements from a single call.


  • Insert into db