3

Why doesn't the following work?

INSERT INTO dbo.Pending_Break 
  (Pending_Pod_ID, Break_Date_Time, Break_Length, Booked_Length)            
OUTPUT INSERTED.Pending_BH_ID -- This is the inserted identity
     , INSERTED.Pending_Pod_ID
     , INSERTED.Break_Name
     , INSERTED.Break_Date_Time
     , pb.PENDING_BH_ID  -- complains on this one
  INTO #InsertedPendingBreaks
 SELECT ippod.Pending_Pod_ID,  
        pb.Break_Date_Time
        pb.break_length,  
        0
   FROM PendingBreak pb
   JOIN InsertedPod ippod ON ...

Can I not use anything other than Inserted or Deleted in the OUTPUT clause?

AngryHacker
  • 59,598
  • 102
  • 325
  • 594

2 Answers2

3

Can I not use anything other than Inserted or Deleted in the OUTPUT clause?

No you can't. At least not with an insert. In SQL Server 2008 you can convert your insert to a merge statement instead and there you can use values from the source table in the output clause.

Have a look at this question how to do that in SQL Server 2008. Using merge..output to get mapping between source.id and target.id

Community
  • 1
  • 1
Mikael Eriksson
  • 136,425
  • 22
  • 210
  • 281
0

The inserted and deleted tables are available only in DML triggers. I'm not sure if you just pulled a code snippet out of a trigger, but if that is a standalone batch then it won't work.

Also, there is no updated table. An update is a delete and then an insert for this. deleted contains the old data and inserted contains the new data on an UPDATE.