In an OUTPUT clause in an INSTEAD OF INSERT trigger, is it possible to reference both INSERTED tables?
SQL Server 2005+
I have a view with an INSTEAD OF INSERT
trigger. Inside the body of the trigger, I want to use a statement with an OUTPUT
clause which references both INSERTED
tables:
- the outer
INSERTED
table for theINSTEAD OF INSERT
trigger - the inner
INSERTED
table for theOUTPUT
clause
MSDN says this:
If a statement that includes an OUTPUT clause is used inside the body of a trigger, table aliases must be used to reference the trigger inserted and deleted tables to avoid duplicating column references with the INSERTED and DELETED tables associated with OUTPUT.
But aliasing doesn't seem to work:
CREATE TRIGGER v_insert ON v
INSTEAD OF INSERT
AS BEGIN
INSERT开发者_如何学编程 INTO t (a, b, c)
OUTPUT inserted.a, inserted.b, outer_inserted.d INTO t_prime (a, b, d)
SELECT a, b, c
FROM inserted as outer_inserted
END
It produces the error "The multi-part identifier "outer_inserted.d" could not be bound. Does that mean what I'm trying to do is not possible?
I read it as the INSERTED alias would be required in the FROM where you access the trigger INSERTED.
The INSERTED in the OUTPUT clause can only reference the data inserted into t.
So you can't have outer_inserted.d
in your OUTPUT clause
Nor can you do this, which is how I read it
INSERT INTO t (a, b, c)
OUTPUT inserted.a, inserted.b INTO t_prime (a, b)
SELECT a, b, c
FROM inserted --no alias = **FAIL**
This is a little bit old but you are missing the d column from the SELECT from outer_inserted table so you can't reference it in OUTPUT.
精彩评论