I am migrating data from FoxPro to SQL. Generally easy enough, but the formats are a little different. So, I dumped FoxPro data into staging tables with SSIS, and now I need to massage the data into a header/detail type situation. To avoid RBAR, I am trying to get this method to work. Am I missing something? Is there a better way to do this?
Declare @Source Table ( SourceId int Identity(1,1) ,OtherId int ,Val1 varchar(10) ,Val2 varchar(10) ) Declare @Destination Table ( DestinationId int Identity(1,1) ,Col1 varchar(10) ,Col2 varchar(10) ) Declare @Output Table ( DestinationId int ,SourceId int ) Insert @Source Values (10, 'abc', 'def') ,(15, 'xsy', 'werpiou') ,(50, 'foo', 'bar') --Insert @Destination --Output Inserted.DestinationId -- ,@Source.SourceId --Into @Output --Select Val1 -- ,Val2 --From @Source s Insert @Output Select mrg.DestinationId ,mrg.SourceId From ( Merge @Destination d Using @Source s on 0 = 1 When Not Matched Then Insert (Col1 ,Col2) Values (Val1 ,Val2) Output d.DestinationId ,s.SourceId ) mrg ( DestinationId ,SourceId ) --Desired in @Output Select 1 DestinationId ,10 SourceId Union All Select 2 ,15 Union All Select 3 ,50
I am using SQL2008 R2 (SP2). I get an error on the Merge statement:
Msg 4104, Level 16, State 1, Line 45
The multi-part identifier "d.DestinationId" could not be bound.