This trigger:
ALTER TRIGGER InsteadTrigger on CustomerView
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO Person
SELECT FirstName, LastName
FROM inserted
Causes this query to return null.
INSERT INTO CustomerView (FirstName, LastName) Values ('Jonathan', 'Allen')
SELECT SCOPE_IDENTITY()
Is there a way to fix the trigger so that SCOPE_IDENTITY will return the correct value? (I can’t use @@IDENTITY because there may be other triggers on the table involved.)
No I don’t think this is possible.
You can return it from the trigger itself of course.
And you can use
CONTEXT_INFOto suppress the result set except for when you require it.Using the
OUTPUTclause doesn’t work in this context as discussed in this Microsoft Connect Item it is evaluated before the insert into the main table and so before theIDENTITYfunction is called.