Primary key value after insertion of row in SQL Server 2005

旧城冷巷雨未停 提交于 2019-12-01 22:18:07

By all means - use the SCOPE_IDENTITY() if your ID column is an INT IDENTITY - only that will give you the correct results!

The first approach with the MAX(ID) will fail terribly if you have multiple clients inserting rows almost at the same time - you'll get false results back. Don't use that!

The third approach might fail if another entry with the same values for E-Mail and name already exists.

Also, as a side-note: you should never use sp_ as your prefix! This is a Microsoft-reserved prefix and has downsides in terms of performance - use something else.

If you have an Identity column as primary key you should use SCOPE_IDENTITY()

You could also use the OUTPUT Clause to return the ID.

insert into tableTest(testUserEmail,testUserName) 
output inserted.ID
values (@testValue, @testName)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!