?
Solved

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

Posted on 2006-11-16
8
Medium Priority
?
1,167 Views
Last Modified: 2009-10-26
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.
0
Comment
Question by:mike_marquet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 6

Expert Comment

by:so3
ID: 17955730
Use IDENT_CURRENT('table_name') to get the Identity value.
0
 
LVL 20

Expert Comment

by:Sirees
ID: 17955774
From BOL:

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

IDENT_CURRENT -
Returns the last identity value generated for a specified table in any session and any scope.
0
 

Author Comment

by:mike_marquet
ID: 17955815
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
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 6

Expert Comment

by:so3
ID: 17956169
try to use an OUTPUT parameter instead.
0
 

Author Comment

by:mike_marquet
ID: 17956460
what do you mean by : try to use an OUTPUT parameter instead. ?
0
 
LVL 6

Expert Comment

by:so3
ID: 17958730
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 http://www.awprofessional.com/articles/article.asp?p=25288&seqNum=9&rl=1
or
http://www.sqlteam.com/item.asp?ItemID=2644



0
 

Author Comment

by:mike_marquet
ID: 17963308
If have found the solution.

The query to get the identity must be changed from

     "SELECT SCOPE_IDENTITY()"

to

     "SET NOCOUNT ON SELECT SCOPE_IDENTITY()"
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 0 total points
ID: 18006139
PAQing question with refund
angelIII, page editor
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question