Explicitly inserting data into an IDENTITY column  Reserving a block of IDENTITY column values

Chapter 7: Adding, Changing, and Deleting Data

Retrieving IDENTITY column values with @@identity

Use the @@identity global variable to retrieve the last value inserted into an IDENTITY column. The value of @@identity changes each time an insert, select into, or bcp statement attempts to insert a row into a table. @@identity does not revert to its previous value if the insert, select into, or bcp statement fails, or if the transaction that contains it is rolled back. If the statement affects a table without an IDENTITY column, @@identity is set to 0.

The value for @@identity within a stored procedure or trigger does not affect the value outside the stored procedure or trigger. For example:

select @@identity
--------------------------------------- 
                                   101
create procedure reset_id as
    set identity_insert sales_daily on
    insert into sales_daily (syb_identity, stor_id) 
         values (102, "1349")
    select @@identity
select @@identity
execute reset_id
--------------------------------------- 
                                  102
select @@identity
--------------------------------------- 
                                  101




Copyright © 2005. Sybase Inc. All rights reserved. Reserving a block of IDENTITY column values

View this book as PDF