(forgive me – I’m new to both StackOverflow & SQL)
Tl;dr – When using @@identity (or any other option such as scope_identity or output variable), is it possible to also use a view? Here is an example of a stored procedure using @@identity:
--SNIP--
DECLARE @AID INT
DECLARE @BID INT
INSERT INTO dbo.A (oct1)
VALUES
(@oct1)
SELECT @AID = @@IDENTITY;
INSERT INTO dbo.B (duo1)
VALUES
(@duo2)
SELECT @BID = @@IDENTITY
INSERT INTO dbo.tblAB (AID, BID)
VALUES
(@AID, @BID)
GO
Longer:
When inserting into a table, you can capture the current value of the identity seed using @@identity. This is useful if you want to insert into table A and B, capture the identity value, then insert into table AB relating A to B. Obviously this is for purposes of data normalization.
Let’s say you were to abstract the DB Schema with a few that performs inner joins on your tables to make the data easier to work with. How would you populate the cross reference tables properly in that case? Can it be done the same way, if so, how?
Avoid using @@IDENTITY or SCOPE_IDENTITY() if your system is using Parallel plans as there is a nasty bug. Please refer –
http://connect.microsoft.com/SQL/feedback/ViewFeedback.aspx?FeedbackID=328811
Better way to fetch the inserted Identity ID would be to use OUTPUT clause.
EDIT:
It would work with Views as well. Please see the sample below. Hope this is what you were looking for.