Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2006
    Posts
    57

    Unanswered: Is there a way to return the PK ID of the row inserted?

    When I insert a row I need the PK assigned to that row. Is there a 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
    Jan 2007
    Posts
    3
    Hi,
    Following are the ways to find out last Identity value inserted.
    @@Identity
    Scope_Identity()
    Ident_Current()

    See which one is useful to you.
    One thing to remember, these solutions work only when the primary key is an identity column.

  4. #4
    Join Date
    May 2004
    Location
    Seattle
    Posts
    1,313
    almost always the one you want to use is scope_identity()

    read BOL on the differences between these three:

    http://msdn2.microsoft.com/en-us/library/ms190315.aspx

  5. #5
    Join Date
    Mar 2005
    Location
    Netherlands
    Posts
    280
    If you're using SQL 2005 you can use the OUTPUT-clause:
    Code:
    INSERT MyTable (<column list>)
    OUTPUT INSERTED.MyID
    VALUES (<insert list>)

Posting Permissions

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