Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Can SELECT IDENT_CURRENT(TableName) be used outside a stored procedure?


All examples I saw in msdn were stored procedure related.Is it that it is meant only for stored procedures or not?
Posted

1 solution

No, you can use it in "normal" programming as well.
I would advise against it though: it is "any session, any scope" = which means it may or may not relate to anything you have done, but could be a different user action entirely. Use @@IDENTITY instead if you can.
There is a non-SP example on MS here: http://technet.microsoft.com/en-us/library/aa933217(v=sql.80).aspx[^]
 
Share this answer
 
Comments
Mike Meinz 19-Mar-14 12:50pm    
Agree but I always thought SCOPE_IDENTITY was better to use than @@IDENTITY because SCOPE_IDENTITY returned value from the current scope.

The following is from @@IDENTITY documentation:
@@IDENTITY and SCOPE_IDENTITY will return the last identity value generated in any table in the current session. However, SCOPE_IDENTITY returns the value only within the current scope; @@IDENTITY is not limited to a specific scope.
...
The scope of the @@IDENTITY function is the local server on which it is executed.
OriginalGriff 19-Mar-14 12:58pm    
Yep - but only for the current session, it doesn't include "other users" changes, which makes it closer to the intent of IDENT_CURRENT than SCOPE_IDENTITY: I guess it depends exactly what you are trying to do!
Mike Meinz 19-Mar-14 13:01pm    
Yes, OP's intent is not clear.

I use SCOPE_IDENTITY exclusively because I have always only had the need to get the id of the row that I just inserted not the row id that some other random process inserted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900