12

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?

Peter Turner
  • 11,199
  • 10
  • 68
  • 109

3 Answers3

18

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()
Jose Basilio
  • 50,714
  • 13
  • 121
  • 117
13
SELECT CAST( bigintcolumn AS int )

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

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
4

Just cast this like:

select CAST(SCOPE_IDENTITY() as int)

And your data Layer:

reader.GetInt32(0);
davioooh
  • 23,742
  • 39
  • 159
  • 250