title: Using MERGE into a view with an INSTEAD OF trigger and OUTPUT clause
body: <p>I'm trying to use MERGE on a view that has an INSTEAD OF trigger and also output the identity value that was inserted, but I get the error "The column reference "inserted.Whatever" is not allowed because it refers to a base table that is not being modified in this statement. It seems to error any time I use inserted in the OUTPUT clause.</p>
  
  <p>A highly simplified example:</p>
  
  <pre><code>/* Cleanup
  DROP VIEW Test_1;
  DROP TABLE Test1;
  */
  GO
  CREATE TABLE Test1
  (
      FirstName   NVARCHAR(50),
      LastName    NVARCHAR(50),
      ID          INT IDENTITY(1, 1)
  );
  GO
  CREATE VIEW Test_1
  AS
  SELECT FirstName, LastName, ID
  FROM Test1;
  GO
  CREATE TRIGGER tiu_Test_1 ON Test_1
    INSTEAD OF INSERT, UPDATE
  AS
  
  MERGE INTO Test1 T
  USING(SELECT * FROM inserted) S
  ON T.ID = S.ID
  WHEN MATCHED THEN
      UPDATE
      SET FirstName = S.FirstName,
          LastName = S.LastName
  WHEN NOT MATCHED BY TARGET THEN
      INSERT(FirstName, LastName)
      VALUES(S.FirstName, S.LastName);
  GO
  -- Test MERGE
  DECLARE @test   TABLE
  (
      ID  INT
  );
  MERGE INTO Test_1 T
  USING(VALUES(N'Jules', N'Vern'))
  AS S(FirstName, LastName)
  ON T.FirstName = S.FirstName
    AND T.LastName = S.LastName
  WHEN NOT MATCHED BY TARGET THEN
      INSERT(FirstName, LastName)
      VALUES(S.FirstName, S.LastName)
  OUTPUT inserted.ID INTO @test;
  </code></pre>
  
  <p>I know this example doesn't actually need the INSTEAD OF trigger. Without the trigger, it works fine, but my actual use case is selecting from and updating multiple tables.</p>
  
  <p>The reason I'm using MERGE instead of the normal INSERT/UPDATE is that I want to match the identity value with a key that's not being inserted. MERGE lets me do this, but INSERT does not.</p>
  
  <p>I'm using SQL Server 2012.</p>
  
  <p>Is this possible?</p>