How do I cast Scope_Identity() to Int?

泪湿孤枕 提交于 2019-12-22 03:41:28

问题


So Scope_Identity() returns an ?-Byte Numeric Type in SQL Server.

That is not awesome.

Is there a safe way to cast it to an int in a select query so we don't have to manage every whim of SQL Server in our ODBC Wrapper?


回答1:


If the source column to which the identity belongs is an Integer, there's no need to cast it. The following works just fine assuming the identity column is an Integer to begin with or it fits inside an "Int".

DECLARE @NewIdent Int
SET @NewIdent = SCOPE_IDENTITY()



回答2:


SELECT CAST( bigintcolumn AS int )

(Provided you know it will fit into a 32bit integer)




回答3:


Just cast this like:

select CAST(SCOPE_IDENTITY() as int)

And your data Layer:

reader.GetInt32(0);


来源:https://stackoverflow.com/questions/1001854/how-do-i-cast-scope-identity-to-int

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