Returning Identity with Invoke-Sqlcmd ?

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Kitt Holland Kitt Holland 4 years, 1 month ago.

  • Author
    Posts
  • #4793
    Profile photo of Kitt Holland
    Kitt Holland
    Participant

    I'm doing an insert into a MSSQL 2012 database using the SQLPS module and Invoke-Sqlcmd. The table I'm inserting into has an auto-incrementing identity column. Does anyone know how to return or select the identity of the row that I inserted?

  • #4794
    Profile photo of Jeffrey Smith
    Jeffrey Smith
    Participant

    This Stack Overflow post seems to have a LOT of good information on the topic:

    http://stackoverflow.com/questions/42648/best-way-to-get-identity-of-inserted-row

  • #4795
    Profile photo of Kitt Holland
    Kitt Holland
    Participant

    Thanks for the link, I did look at that thread and Scope_Identity always returned NULL for me. I assume because it's occurring as a separate session/connection since I have to do a separate Invoke-Sqlcmd -query "Select Scope_Identity();". If that is the correct way I'm not sure what the syntax would be.

You must be logged in to reply to this topic.