views:

24

answers:

2

i am currently calling SELECT @@identity from VBA in mysql:

Set rs = cn.Execute("SELECT @@identity", , adCmdText)

but since i am going to be working with sql server db instead of mysql, i would like to know how to make this statement sql-server friendly

would it just be Set rs = cn.Execute("SCOPE_IDENTITY()", , adCmdText) ??

A: 

If you're asking about the T-SQL side of it, then you should be able to just use the same statement (i.e. 'SELECT @@identity').

Geoff
No never use @@identity
HLGEM
You're right; it works as-is, but it isn't best practice.
Geoff
A: 

Both SQL statements are valid, with one exception. Change

"SCOPE_IDENTITY()"

to

"SELECT SCOPE_IDENTITY()"

The difference between the two is that the @@identity variable contains the most recent identity value on the SQL Server (global perspective). The SCOPE_IDENTITY() function returns the most recent local identity.

You can find more on the SCOPE_IDENTITY here.

bobs
what is local identity?
I__
Local identity represents the identity value within the scope of a batch or procedure.
bobs
Not exactly. @@identity is not global, it is realted to the actual query that ran it won't give the identity for some other query that finsihed a millisecond later and before the call for @@identity. But it will give you the wrong answer amany times and should not be used becasue it willgive the last identity from the chain of events caused by your query or proc inculding an identity generated in a trigger which is not the identity you want. @@Identity shoudl never be used to find the identity value you just inserted in SQL Server
HLGEM
Thanks @HLGEM - better description than I provided.
bobs