Andrew
Andrew

Reputation: 203

The multi-part identifier could not be bound in merge

I am using below merge and getting "The multi-part identifier "MasterSource.monthvrip" could not be bound" error. Checked a lot in internet but no luck . Looks like using some defined keyword but not able to figure out. Any suggestion will be helpful.

USE TestDatabase

MERGE [dbo].[cost1] AS target   
USING (     
  SELECT [monthvrip]
  FROM [dbo].[cost]
) AS MasterSource
ON (MasterSource.[monthvrip] = target.[monthvrip])

WHEN MATCHED AND (MasterSource.[monthvrip] <> target.[monthvrip])
THEN UPDATE 
  SET MasterSource.[monthvrip] = target.[monthvrip]

WHEN NOT MATCHED BY target THEN 
INSERT 
  ([monthvrip])
VALUES 
  (MasterSource.[monthvrip])

OUTPUT
$action,
inserted.*,
deleted.*;

Msg 4104, Level 16, State 1, Line 4

The multi-part identifier "MasterSource.monthvrip" could not be bound.

Upvotes: 0

Views: 2205

Answers (1)

Ethan1701
Ethan1701

Reputation: 193

You need to change

THEN UPDATE 
  SET MasterSource.[monthvrip] = target.[monthvrip]

to

THEN UPDATE 
  SET target.[monthvrip] = MasterSource.[monthvrip]

The way you have it at the moment, you're trying to update the source of the merge, rather than the target.

Upvotes: 3

Related Questions