Why does select SCOPE_IDENTITY() return a decimal instead of an integer?

五迷三道 提交于 2019-11-26 18:52:33

In SQL Server, the IDENTITY property can be assigned to tinyint, smallint, int, bigint, decimal(p, 0), or numeric(p, 0) columns. Therefore the SCOPE_IDENTITY function has to return a data type that can encompass all of the above.

As previous answers have said, just cast it to int on the server before returning it, then ADO.NET will detect its type as you expect.

Can't you just cast it before returning it from your query or stored proc (SPs alway return int anyway, but maybe you are using an output parameter)?

Like SELECT CAST(SCOPE_IDENTITY() AS INT) AS LAST_IDENTITY

And why it does this? Probably to be more flexible and handle larger numbers.

Scope identity return value is decimal(38,0)

CAST it, use the OUTPUT clause, or assign to an output parameter rather than SELECT SCOPE_IDENTITY() to the client

try using this and you'll get an integer back:

ExecuteScalar('insert...; select CONVERT(int,scope_identity())');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!