Tuesday, March 20, 2007

Return IDENTITY value

SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY are similar functions in that they return values inserted into IDENTITY columns.

IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the value generated for a specific table in any session and any scope.


SCOPE_IDENTITY and @@IDENTITY will return last identity values generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.

For example, you have two tables, T1 and T2, and an INSERT trigger defined on T1. When a row is inserted to T1, the trigger fires and inserts a row in T2. This scenario illustrates two scopes: the insert on T1, and the insert on T2 as a result of the trigger.

Assuming that both T1 and T2 have IDENTITY columns, @@IDENTITY and SCOPE_IDENTITY will return different values at the end of an INSERT statement on T1.
@@IDENTITY will return the last IDENTITY column value inserted across any scope in the current session, which is the value inserted in T2.

SCOPE_IDENTITY() will return the IDENTITY value inserted in T1, which was the last INSERT that occurred in the same scope. The SCOPE_IDENTITY() function will return the NULL value if the function is invoked before any insert statements into an identity column occur in the scope.

Failed statements and transactions can alter the current identity for a table and create gaps in the identity column values. The identity value is never rolled back even though the transaction that attempted to insert the value into the table is not committed. For example, if an INSERT statement fails because of an IGNORE_DUP_KEY violation, the current identity value for the table is still incremented.

Examples:
The following example creates two tables, TZ and TY, and an INSERT trigger on TZ. When a row is inserted to table TZ, the trigger (Ztrig) fires and inserts a row in TY.

USE tempdb
GO
CREATE TABLE TZ (
Z_id int IDENTITY(1,1)PRIMARY KEY,
Z_name varchar(20) NOT NULL)

INSERT TZ
VALUES ('Lisa')
INSERT TZ
VALUES ('Mike')
INSERT TZ
VALUES ('Carla')

SELECT * FROM TZ

--Result set: This is how table TZ looks
Z_id Z_name
-------------
1 Lisa
2 Mike
3 Carla

CREATE TABLE TY (
Y_id int IDENTITY(100,5)PRIMARY KEY,
Y_name varchar(20) NULL)

INSERT TY (Y_name)
VALUES ('boathouse')
INSERT TY (Y_name)
VALUES ('rocks')
INSERT TY (Y_name)
VALUES ('elevator')

SELECT * FROM TY
--Result set: This is how TY looks:
Y_id Y_name
---------------
100 boathouse
105 rocks
110 elevator

/*Create the trigger that inserts a row in table TY
when a row is inserted in table TZ*/
CREATE TRIGGER Ztrig
ON TZ
FOR INSERT AS
BEGIN
INSERT TY VALUES ('')
END

/*FIRE the trigger and find out what identity values you get
with the @@IDENTITY and SCOPE_IDENTITY functions*/
INSERT TZ VALUES ('Rosalie')

SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]
GO
SELECT @@IDENTITY AS [@@IDENTITY]
GO

19 comments:

  1. order ativan lorazepam 1 mg half life - happens if overdose ativan

    ReplyDelete
  2. buy ambien buy ambien in canada - coping with ambien withdrawal

    ReplyDelete
  3. ativan no prescription ativan withdrawal hair loss - side effects getting off ativan

    ReplyDelete
  4. order xanax no prescription order xanax from europe - overdose on xanax death

    ReplyDelete
  5. can i buy xanax online xanax xr reviews - alprazolam 0.5 mg street value

    ReplyDelete
  6. buy ativan buy lorazepam in mexico - ativan for alcohol withdrawal

    ReplyDelete
  7. xanax cheap what is xanax withdrawal like - xanax drug slang

    ReplyDelete
  8. cheap ativan online ativan dose for nausea - ativan withdrawal depression

    ReplyDelete
  9. buy xanax online xanax and alcohol together - yellow generic xanax bars

    ReplyDelete
  10. ambien no prescription half life of ambien 10 mg - ambien side effects bad taste mouth

    ReplyDelete
  11. carisoprodol buy somatropin hgh online - how to buy soma online

    ReplyDelete
  12. buy ambien online ambien sleep structure - ambien online no prescription usa

    ReplyDelete
  13. order soma normal dosage carisoprodol - can you buy tickets soma

    ReplyDelete
  14. ambien sleep medication ambien long term - ambien side effects amnesia

    ReplyDelete
  15. buy valium online overnight delivery valium thailand pharmacy - order roche valium online

    ReplyDelete
  16. ambien sleep ambien side effects constipation - ambien pharmacy no prescription

    ReplyDelete
  17. soma for sale buy cheap soma online no prescription - muscle relaxer medication soma

    ReplyDelete
  18. soma pain carisoprodol withdrawal syndrome - carisoprodol grapefruit juice

    ReplyDelete
  19. [url=http://aluejxfttk.com]kPTrGYX[/url] , EDLsCKqi - http://pyfnknfrtw.com

    ReplyDelete

Lets keep comments section also purely technical