SCOPE_IDENTITY problem after migrating from SQL Server 2000 to SQL Server 2005 when using ODBC API

Hi to all,

I have a little problem using the SCOPE_IDENTITY to get the latest identity value inserted after an "INSERT INTO" statement, and this, using the ODBC API.

After inserting the row, I make a "SELECT SCOPE_IDENTITY()" to get the inserted ID but the return value is = 0.

If I use "SELECT @@IDENTITY" to get the identity, the returned ID is correct.

All was working fine under SQL Server 7 and SQL Server 2000 (after MDAC upgrade).

Any idea ?

P.S. : Under Query Analyser, the SCOPE_IDENTITY is working.
Who is Participating?
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
PAQing question with refund
angelIII, page editor
Use IDENT_CURRENT('table_name') to get the Identity value.
From BOL:

Returns the last IDENTITY value inserted into an IDENTITY column in the same scope. A scope is a module -- a stored procedure, trigger, function, or batch. Thus, two statements are in the same scope if they are in the same stored procedure, function, or batch.

Returns the last identity value generated for a specified table in any session and any scope.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

mike_marquetAuthor Commented:
Using IDENT_CURRENT is more complex because I have a generic class (CMyODBC) to access the database and therefore have a function called :

int ExecuteSQL(const char *szQuery, long *pID = NULL)

And don't know what I have in szQuery (INSERT, UPDATE, DELETE, ...) and don't know which table name to set in IDENT_CURRENT

The think I don't understand is WHY it works under SQL Server 7 and 2000 and NOT under SQL Server 2005
try to use an OUTPUT parameter instead.
mike_marquetAuthor Commented:
what do you mean by : try to use an OUTPUT parameter instead. ?
Where do you create the querries?

if you are using stored procedure you can use an output parameter to return a value. You said that in Query Analyser, the SCOPE_IDENTITY is working so the problem is when the value is returned in your app. Can you post the ExecuteSQL method

An example on how to use output parameters can be found at

mike_marquetAuthor Commented:
If have found the solution.

The query to get the identity must be changed from



All Courses

From novice to tech pro — start learning today.