Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2004
    Posts
    38

    Unanswered: Capturing ID when executing an INSERT

    I have a bit of code that executes an INSERT statement to add a record to an existing table. This table of course has an automatically incrementing ID field, and I'd like to somehow have my INSERT statement return the value of that ID field so that I can automatically show the user the record they've added.

    Is there a clean way to do this?

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    yes, there is, the SCOPE_IDENTITY function
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Oct 2004
    Posts
    38
    awesome! exactly what i needed.. thanks!

  4. #4
    Join Date
    Jul 2007
    Posts
    96
    For more information about how to capture identities in SQL Server please read the following article.

    http://www.mssqltips.com/tip.asp?tip=1385

  5. #5
    Join Date
    Sep 2003
    Location
    New York, NY
    Posts
    136
    or if you're using SQL Server 2005, you can use the OUTPUT clause

    http://www.databasejournal.com/featu...le.php/3598636

Posting Permissions

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