Background:
I have a Stored Procedure which need to insert data into a set of tables by maintaining the referential integrity. Meaning parent table ID value (ID is a Identity column) need to be fetched and used in child tables during insertion. There is a trigger defined in the parent table which inserts a record in another table. Logic in the SP is like 1. inserts a record in parent table 2. immediate statement retrieves last identity using @@IDENTITY global variable. As the trigger inserts record in another table, the @@IDENTITY variable returns the identity from second table. See Query-03 which represents the same scenario.
This problem drove me to investigate in detail about the approaches and their pros and cons to retrieve IDENTITY value of a table.
Factors need to consider: (mostly for @@IDENTITY, SCOPE_IDENTITY(), IDENT_CURRENT())
Different scopes:
A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.
What is @@IDENTITY?
What are the alternatives?
-------------
-- Query-01 - OUTPUT clause
-------------
-- Data table
CREATE TABLE test(id INT IDENTITY(1,1), value VARCHAR(100));
-- table for output
DECLARE @outputTable TABLE (id INT);
INSERT INTO test(value)
OUTPUT inserted.id
INTO @outputTable
VALUES('One');
SELECT * FROM test;
SELECT * FROM @outputTable;
-- Test OUTPUT with UPDATE
INSERT INTO test(value) VALUES('Two');
UPDATE test
SET value = 'One Changed'
OUTPUT deleted.id
INTO @outputTable
WHERE id=1
;
SELECT * FROM test;
SELECT * FROM @outputTable;
-- Test OUTPUT with DELETE
DELETE test
OUTPUT deleted.id
INTO @outputTable
WHERE id=2;
SELECT * FROM test;
SELECT * FROM @outputTable;
GO
-------------
-- Query-02 - Output of OUTPUT clause in a variable
-- Result: it fails because OUTPUT returns data in tabular format
-------------
-- Data table
CREATE TABLE test(id INT IDENTITY(1,1), value VARCHAR(100));
DECLARE @outputVariable INT;
INSERT INTO test(value)
OUTPUT inserted.id
INTO @outputVariable
VALUES('One');
SELECT * FROM test;
SELECT 'Variable= '+CONVERT(VARCHAR(100), @outputVariable);
GO
Error:
Msg 1087, Level 16, State 1, Line 6
Must declare the table variable "@outputVariable".
-------------
-- Query-03 - Usage of @@IDENTITY, SCOPE_IDENTITY(), IDENT_CURRENT()
-------------
-- Data tables
CREATE TABLE one(id INT IDENTITY(1,1), value VARCHAR(100));
CREATE TABLE two(id INT IDENTITY(1,1), value VARCHAR(100));
GO
-- Create trigger in table 'one'
CREATE TRIGGER TROne
ON one
AFTER INSERT
AS
DECLARE @value VARCHAR(100), @id INT;
SELECT @value=value, @id=id FROM inserted;
INSERT INTO two(value) VALUES('ID='+CONVERT(VARCHAR(100), @id) + ' is inserted in table one');
INSERT INTO two(value) VALUES('Value='+CONVERT(VARCHAR(100), @value) + ' is inserted in table one');
GO
INSERT INTO one(value) VALUES('Record one');
SELECT '@@IDENTITY= '+CONVERT(VARCHAR(100), @@IDENTITY);
SELECT '@@SCOPE_IDENTITY= '+CONVERT(VARCHAR(100), SCOPE_IDENTITY());
SELECT '@@IDENT_CURRENT for table one= '+CONVERT(VARCHAR(100), IDENT_CURRENT('one'));
SELECT '@@IDENT_CURRENT for table two= '+CONVERT(VARCHAR(100), IDENT_CURRENT('two'));
GO
I have a Stored Procedure which need to insert data into a set of tables by maintaining the referential integrity. Meaning parent table ID value (ID is a Identity column) need to be fetched and used in child tables during insertion. There is a trigger defined in the parent table which inserts a record in another table. Logic in the SP is like 1. inserts a record in parent table 2. immediate statement retrieves last identity using @@IDENTITY global variable. As the trigger inserts record in another table, the @@IDENTITY variable returns the identity from second table. See Query-03 which represents the same scenario.
This problem drove me to investigate in detail about the approaches and their pros and cons to retrieve IDENTITY value of a table.
Factors need to consider: (mostly for @@IDENTITY, SCOPE_IDENTITY(), IDENT_CURRENT())
Different scopes:
- The session scope - the connection which produced the IDENTITY value
- The statement scope - where is the statement that produced the IDENTITY value
- The table scope -the able produced the IDENTITY value
A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.
What is @@IDENTITY?
- @@IDENTITY is a system global variable
- Returns the last IDENTITY value produced on a connection
- Regardless of the table that produced the value
- Regardless of the scope of the statement that produced the value
- Returns IDENTITY of current session, ignores the scope
What are the alternatives?
- Use SCOPE_IDENTITY()
- Attributes:
- 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
- Returns IDENTITY of current session and current scope
- Notes:
- IDENTITY created in Trigger or User Defined Function for other table(s) won't affect actual table's IDENTITY
- (see Query-03 for sample code)
- Use IDENT_CURRENT('tablename')
- Attributes:
- Returns last IDENTITY produced in a table
- Regardless of the connection that created the value
- regardless of the scope of the statement that produced the value
- Notes:
- IDENTITY created in Trigger or User Defined Function for other table(s) won't affect actual table's IDENTITY
- If IDENTITY created in current scope is needed, this might not return correct value as some other connection might have already entered record in between
- (see Query-03 for sample code)
- Use OUTPUT clause:
- Attributes:
- Returns information from, or expressions based on, each row affected by an INSERT, UPDATE, DELETE (see Query-01)
- Information can be retrieved in a tabular form only, cannot be retrieved in a variable (see Query-02)
- Columns returned from OUTPUT reflect the data as it is after the INSERT, UPDATE, or DELETE statement has completed but before triggers are executed
- Notes:
- The Output clause is a new to SQL Server 2005
- An UPDATE, INSERT, or DELETE statement that has an OUTPUT clause will return rows to the client even if the statement encounters errors and is rolled back. The result should not be used if any error occurs when you run the statement
- Use SEQUENCE feature in column (similar as Oracle Sequence)
- Notes:
- Available in SQL Server 2011 only
- I couldn't come up with sample code as not used SQL Server 2011 yet
- Use custom sequence (a table to store sequence value and a Stored Procedure to modify the value)
- Notes:
- Custom logic
- Concurrency need to be handled manually
- How to use OUTPUT clause - http://msdn.microsoft.com/en-us/library/ms177564.aspx
- Problem in SCOPE_IDENTITY - (1) https://connect.microsoft.com/SQLServer/feedback/details/328811/scope-identity-sometimes-returns-incorrect-value (2) http://support.microsoft.com/kb/2019779
-------------
-- Query-01 - OUTPUT clause
-------------
-- Data table
CREATE TABLE test(id INT IDENTITY(1,1), value VARCHAR(100));
-- table for output
DECLARE @outputTable TABLE (id INT);
INSERT INTO test(value)
OUTPUT inserted.id
INTO @outputTable
VALUES('One');
SELECT * FROM test;
SELECT * FROM @outputTable;
-- Test OUTPUT with UPDATE
INSERT INTO test(value) VALUES('Two');
UPDATE test
SET value = 'One Changed'
OUTPUT deleted.id
INTO @outputTable
WHERE id=1
;
SELECT * FROM test;
SELECT * FROM @outputTable;
-- Test OUTPUT with DELETE
DELETE test
OUTPUT deleted.id
INTO @outputTable
WHERE id=2;
SELECT * FROM test;
SELECT * FROM @outputTable;
GO
-------------
-- Query-02 - Output of OUTPUT clause in a variable
-- Result: it fails because OUTPUT returns data in tabular format
-------------
-- Data table
CREATE TABLE test(id INT IDENTITY(1,1), value VARCHAR(100));
DECLARE @outputVariable INT;
INSERT INTO test(value)
OUTPUT inserted.id
INTO @outputVariable
VALUES('One');
SELECT * FROM test;
SELECT 'Variable= '+CONVERT(VARCHAR(100), @outputVariable);
GO
Error:
Msg 1087, Level 16, State 1, Line 6
Must declare the table variable "@outputVariable".
-------------
-- Query-03 - Usage of @@IDENTITY, SCOPE_IDENTITY(), IDENT_CURRENT()
-------------
-- Data tables
CREATE TABLE one(id INT IDENTITY(1,1), value VARCHAR(100));
CREATE TABLE two(id INT IDENTITY(1,1), value VARCHAR(100));
GO
-- Create trigger in table 'one'
CREATE TRIGGER TROne
ON one
AFTER INSERT
AS
DECLARE @value VARCHAR(100), @id INT;
SELECT @value=value, @id=id FROM inserted;
INSERT INTO two(value) VALUES('ID='+CONVERT(VARCHAR(100), @id) + ' is inserted in table one');
INSERT INTO two(value) VALUES('Value='+CONVERT(VARCHAR(100), @value) + ' is inserted in table one');
GO
INSERT INTO one(value) VALUES('Record one');
SELECT '@@IDENTITY= '+CONVERT(VARCHAR(100), @@IDENTITY);
SELECT '@@SCOPE_IDENTITY= '+CONVERT(VARCHAR(100), SCOPE_IDENTITY());
SELECT '@@IDENT_CURRENT for table one= '+CONVERT(VARCHAR(100), IDENT_CURRENT('one'));
SELECT '@@IDENT_CURRENT for table two= '+CONVERT(VARCHAR(100), IDENT_CURRENT('two'));
GO
No comments:
Post a Comment