Alternatives to @ identity in SQL Server 2000

Source: Internet
Author: User
Alternatives to @ identity in SQL Server 2000
@ Www_transtaafl_com 2006-03-05
--------------------------------------------------------------------------------
This article describes in detail how to obtain the records just inserted in SQL Server 2000.
--------------------------------------------------------------------------------
Mark Writes: "I was asked a question today by a developer having problems getting the right identity value from @ identity when a table has a trigger which has an additional insert-post the insert statement the select @ @ identity returns the wrong value (which is behaviour I wocould CT ).

Is there a trick to get round this, apart from not using triggers and/or not using identity columns-which is what I suggested ..."

Prior to SQL Server 2000, the answer was "no ". however, SQL Server 2000 adds two cool new functions to help you get around this problem. read on... SQL Server 2000 has three functions that return identity information. the result of each of these three functions is dependent on three factors:

    • The session scope (which connection produced the identity value ?)
    • The table scope (which table produced the identity value ?)
    • The statement scope (where is the statement that produced the identity value ?)

(SQL statements that are contained in the same batch, stored procedure, or trigger are considered to be in the same scope. so, if I call an insert that fires a trigger, I have two different scopes: Scope 1 is inside the batch that called the insert, and scope 2 is inside the trigger .)

Select@ Identity
This is everyone's favorite function, unchanged from earlier versions of SQL Server. it returns the last identity value produced on a connection, regardless of the table that produced the value, and regardless of the scope of the statement that produced the value.

SelectIdent_current ('Tablename')
This new function returns the last identity value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value.

SelectScope_identity ()
This new function returns the last identity value produced on a connection and by a statement in the same scope, regardless of the table that produced the value.

Okay, I think these are best explained with an example, and since Mark has provided a good one, we'll use it.
Let's create some tables:

Create Table yakname (ID int identity (), yakname varchar (30) Create Table yaktracker (ID int identity (), trantype char (1), yakname varchar (30 )) go
 
Create trigger ti_yak on yakname for insertasbegin insert yaktracker (trantype, yakname) Select 'I, yakname from insertedendgo

This is a basic scenario that duplicates the problem mark described. whenever I insert a record (s) into yakname, the trigger will insert a record (s) into yaktracker (We Need Audit information on the yaks, apparently ).

Now, Mark's developer is trying something like this:

Insert yakname values ('graz ')
Select @ identity

Although what we really want from this batch of statements is the last identity value for the yakname table, we get the value 1000 -- the last identity value

For the yaktracker table. Why is this? The second insert statement in the trigger also inserts into a table with an identity. remember, @ identity works authentication SS all tables (yakname, yaktracker) and all scopes (batch scope, trigger scope), so it picked up the change we made to a different table (yaktracker) in a different scope (the trigger )!

Prior to SQL Server 2000, we wowould have been stuck. at this point, we 'd have to eliminate the identity column on the yaktracker table to get these statements working the way we want them.

But let's say we 've ve shelled out of the cash for the latest copy of SQL Server 2000. Let's look at how the new functions wowould behave:

Insert yakname values ('Billy Joe bob ')
Select scope_identity () -- returns the value 2

Select ident_current ('yakname') -- returns the value 2 (maybe)
Select ident_current ('yaktracker') -- returns the value 1001 (maybe)

Scope_identity () works for all tablesIn the scope for which it was called, Which in this case is the original batch. So, we get the last value for the yakname table, which is what we wanted.

Note that I have ded some samples for ident_current as well. unless someone else on another connection is also inserting values into the yakname table, you will get the results shown in the example. (remember that the ident_current function disregards which connection produced the last identity value for the specified table .)

Admittedly, these are some pretty handy functions. Of course, I saw these and wanted more... wouldn't it be nice to have a rowset function that returnedAllOf the identity values created because of a multi-row insert? Oh well, I guess we have to wait through another couple of versions to see that feature.

-Sqlguru

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.