Library tutorials & articles
Identifying Your New Records
- Introduction
- Sql Server
- Microsoft Access
Sql Server
SQL Server 2000 offers three, count 'em, three options for retrieving the Id of a recently added record. The 'best' method depends on what you're doing and what else might be going on in your database. In all cases, the Id is a field marked as the primary key of a table and that has the Identity property set to True.
@@IDENTITY
|
This is an intrinsic variable in SQL Server that contains the Id of the record that was last created on this connection, regardless of the table. |
IDENT_CURRENT('table')
|
This function returns the ID of the record that was last created in the specified table. |
SCOPE_IDENTITY
|
This variable contains the Id of the last record that was created within the scope of the current procedure, regardless of the table. |
So what are the ramifications of using each of these options? With @@IDENTITY,
you get the most recent identity on the current connection. That means that
if, as part of the INSERT statement, triggers get executed that in turn INSERT
records
into other tables, @@IDENTITY contains the last Id that was generated. Regardless
of the table into which the record was inserted. Now if you created all of
the stored procedures, triggers and INSERT statements yourself, that is fine.
But
what happens if, sometime in the future, another developer, who is unaware
that @@IDENTITY is being used, adds a trigger that adds an Audit log record.
All of
a sudden, your application will break even though 'nothing' has changed.
And that is the kind of bug that we all just love to track down.
The IDENT_CURRENT function is best used when you can control who has access
to the database and when. By specifying the table as part of the function,
you caneliminate
the issues associated with @@IDENTITY. Unfortunately, in a busy environment,
you can't be sure that between the execution of your INSERT command and the
retrieval of IDENT_CURRENT, that a different record wasn't inserted by another
user.
The SCOPE_IDENTITY instrinsic variable addresses some of the issues raised
with the other two methods. Its value is the last Id created within the current
scope.
The scope would typically be a stored procedure. Now you don't have to worry
about the possibility of future enhancements 'breaking' your code, nor do
you have to deal with other activity impacting the Id that is returned. If
you perform
more that one INSERT in a stored procedure, you do need to use the SCOPE_IDENTITY between each statement in order to retreive all of the created Id's. But
again, that is within your sphere of control.
Unfortunately, if you are using SQL Server 7.0 or earlier, then the @@IDENTITY method is the only choice available to you. Both IDENT_CURRENT and SCOPE_IDENTITY were introduced with SQL Server 2000.
Related articles
Related discussion
-
Send mail from stored Procedure.
by Mulish Mehdi (3 replies)
-
how to update multiple entries
by khari6579 (1 replies)
-
how to create sql 2000 database setup?
by sathyan_8294 (0 replies)
-
Can't Get SQL 2005 CLR to Work!
by rajantk (1 replies)
-
How can images be embedded into an email generated using Database Mail?
by ilocanux (0 replies)
Related podcasts
-
ADO.NET "Astoria" Data Services with Shawn Wildermuth
Scott chats with Shawn Wildermuth, "the ADO Guy," about ADO.NET Data Services, aka "Project Astoria." It's REST for SQL Server. Should you care? What's REST? How does this relate to WCF or ASP.NET?
I'm writing a small application and am having some positioning challenges with a continuous form which inserts new entries into the record set using a combo box.
I have a Microsoft buggie feature in this mode, where each new entry - as I move to the next record - reverts to identical form content to the previous line. Let's say the last entry in the form was for "Orange Juice". Basically, if I select "Grapefruite Juice" as the next entry, then tab on down to the next line and enter "Cranberry", I end up with the last three lines on the form reading "Orange Juice". The record IS accurate - when I requery, the data is there. However, this makes data entry clunky and unprofessional. Can't seem to fix the form.
My fix has been to add a little code that requeries. Naturally, the issue then is that the form resorts and the cursor / bookmark returns to the default first entry in the form. That makes data entry slow and jerky, plus you lose sight of the last lines entered for reference.
Be nice if the form read out correctly line after line - OR, I could find a way to requery, then drop the cursor on the last record entered. Then the user could see pretty graphically, and logically, where their new entry fell in the sort order.
Any ideas?
Aaron
This thread is for discussions of Identifying Your New Records.