views:

21

answers:

1

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 the INSTEAD OF INSERT trigger
  • the inner INSERTED table for the OUTPUT 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?

+3  A: 

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**
gbn
Yeah, it seems the important point is that only DELETE, UPDATE and MERGE statements can reference tables other than INSERTED in the OUTPUT clause. In an INSERT statement it can reference only INSERTED.
John