title: Using OUTPUT clause to insert value not in INSERTED
body: <p>I am writing a stored procedure to process a table belonging to an application and insert values into a table belonging to the same application (so I cannot amend either table).</p>
  
  <p>I have to process only new records and remember which records have been processed, for this I have created a third simple table.</p>
  
  <p>Tables are below, many columns removed to leave only important details.</p>
  
  <p><strong>Source Table</strong></p>
  
  <pre><code>CREATE TABLE [dbo].[DETAIL](
      [DET_NET] [float] NULL,
      [DET_VAT] [float] NULL,
      [DET_VATCODE] [varchar](4) NULL,
      [DET_GROSS] [float] NULL,
      [DET_DATE] [datetime] NULL,
      [DET_PRIMARY] [float] NOT NULL
  )
  </code></pre>
  
  <p><strong>Target Table</strong></p>
  
  <pre><code>CREATE TABLE [dbo].[TRN_TEMP](
      [TRN_TRAN_DATE] [datetime] NULL,
      [TRN_DESCRIPTION] [varchar](20) NULL,
      [TRN_PRIMARY] [int] NULL,
      [TRN_AMT] [float] NULL
  )
  </code></pre>
  
  <p><strong>Tracking Table</strong></p>
  
  <pre><code>CREATE TABLE REGISTER(
      LINE_ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL,
      DET_PRIMARY_LINK FLOAT NOT NULL,
      INS_DATE DATETIME NOT NULL
  )
  </code></pre>
  
  <p>I am trying to insert into the Target table values from the source table, but also insert the primary key of the source table into the tracking table.</p>
  
  <pre><code>INSERT INTO TRN_TEMP (TRN_TRAN_DATE, TRN_DESCRIPTION, TRN_AMT)
  OUTPUT D.DET_PRIMARY, GETDATE() INTO REGISTER (DET_PRIMARY_LINK, INS_DATE)
  SELECT D.DET_DATE, 'SOMETEXT', SUM(D.DET_NET)
  FROM DETAIL D
  LEFT JOIN REGISTER R ON D.DET_PRIMARY = R.DET_PRIMARY_LINK
  WHERE &lt;MY CONDITIONS&gt; AND R.LINE_ID IS NULL -- TO REMOVE LINES ALREADY PROCESSED
  GROUP BY D.DET_DATE
  </code></pre>
  
  <p>I can't see a problem with the text above but I get an error "The multi part identifier 'D.DET_PRIMARY' could not be bound.".
  I've tried both <code>D.DET_DETAIL</code> and <code>DETAIL.DET_DETAIL</code> and the error is the same.</p>
  
  <p>Is it not possible to use values from the source table in the <code>OUTPUT</code> clause when using a group or do I have an error in the formatting?
  If it's not possible is there another way of keeping track of which lines I have processed?</p>