SQL - Inserting a row and returning primary key

前端 未结 8 1165
南笙
南笙 2020-12-01 18:07

I have a little witty problem. Say, I inserted a row with some data in a table where a primary key is present. How would one \"SELECT\" the primary key of the row one just i

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 18:19

    For SQL Server 2005 and up, and regardless of what type your primary key is, you could always use the OUTPUT clause to return the values inserted:

    INSERT INTO dbo.YourTable(col1, col2, ...., colN)
    OUTPUT Inserted.PrimaryKey
    VALUES(val1, val2, ....., valN)
    

提交回复
热议问题