Sql Server return the value of identity column after insert statement

前端 未结 7 672
甜味超标
甜味超标 2020-12-13 17:44

Is it possible in sql server using stored procedure to return the identity column value in a table against which some values are inserted? For example using stored procedure

相关标签:
7条回答
  • 2020-12-13 18:25
    Insert into TBL (Name, UserName, Password) Output Inserted.IdentityColumnName
     Values ('example', 'example', 'example')
    
    0 讨论(0)
  • 2020-12-13 18:29

    send an output parameter like

    @newId int output
    

    at the end use

        select @newId = Scope_Identity() 
    
         return @newId 
    
    0 讨论(0)
  • 2020-12-13 18:32
    SELECT SCOPE_IDENTITY()
    

    after the insert statement

    Please refer the following links

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

    0 讨论(0)
  • 2020-12-13 18:32

    You can use Scope_Identity() to get the last value.

    Have a read of these too:

    • http://www.mikesdotnetting.com/Article/54/Getting-the-identity-of-the-most-recently-added-record
    • Stored procedure - return identity as output parameter or scalar
    0 讨论(0)
  • Here goes a bunch of different ways to get the ID, including Scope_Identity:

    https://stackoverflow.com/a/42655/1504882

    0 讨论(0)
  • 2020-12-13 18:39

    You can use SELECT @@IDENTITY as well

    0 讨论(0)
提交回复
热议问题