开发者

Using OUTPUT with joined tables

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
     , INSERT开发者_如何学PythonED.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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜