How do I cast Scope_Identity() to Int?

Peter Turner picture Peter Turner · Jun 16, 2009 · Viewed 11.5k times · Source

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?

Answer

Jose Basilio picture Jose Basilio · Jun 16, 2009

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()