Avatar of spudmcc
spudmccFlag for United States of America asked on

Merge statement with condition

Hi Experts

I have a statement that was created that finds and replaces data from one table to another.  This works quite well and does the trick.  We now need to add condition.  We want to do the update and add that it only be a specific id that is updated.  

The code that we currently use now is:

MERGE INTO [NYP-RetailDataSQL].dbo.[Sales-Comparison0830A]
   USING [NYP-RetailDataSQL].dbo.wholereplace
      ON md_Retailer = [Cust Id]
WHEN MATCHED THEN
   UPDATE
      SET WholesaleID = WHOLEID;


What I need to add is that I only want it to be when the WHOLEID = 120-MW.  

Any help would be greatly appreciated as always.  

A
Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
Kevin Cross

8/22/2022 - Mon
awking00

I think you can just modify your using statement to a subquery
 USING (SELECT * FROM [NYP-RetailDataSQL].dbo.wholereplace WHERE WHOLEID = 120-MW)
ASKER
spudmcc

Errored out "syntax error near "on"


MERGE INTO [NYP-RetailDataSQL].dbo.[Sales-Comparison0830A]
   USING(SELECT * FROM [NYP-RetailDataSQL].dbo.wholereplace
           WHERE WholesaleID  = 120 - MW)
  ON md_Retailer = [Cust Id]
WHEN MATCHED THEN UPDATE
SET WholesaleID = WHOLEID;
Kevin Cross

I think you are just missing an alias for the derived table.

MERGE INTO [NYP-RetailDataSQL].dbo.[Sales-Comparison0830A]
   USING(SELECT * FROM [NYP-RetailDataSQL].dbo.wholereplace
           WHERE WholesaleID  = '120 - MW') tfr ON md_Retailer = [Cust Id]
WHEN MATCHED THEN UPDATE
SET WholesaleID = WHOLEID;

You also can use a common table expression.

;WITH tfr AS (
    /* specify columns to retrieve versus asterisk. */
    SELECT *
    FROM [NYP-RetailDataSQL].dbo.wholereplace
    WHERE WholesaleID  = '120 - MW'
)

MERGE INTO [NYP-RetailDataSQL].dbo.[Sales-Comparison0830A]
    USING tfr ON md_Retailer = [Cust Id]
    WHEN MATCHED THEN UPDATE
        SET WholesaleID = WHOLEID;

Regards,

Kevin

EDIT: I think you were missing quotes around the WholesaleID literal also.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
spudmcc

SQL1.sql: Error (4,1): Incorrect syntax near the keyword 'WHERE'.
Kevin Cross

Please show the exact SQL you tried that generated the above error. Paste into a code snippet.
ASKER
spudmcc

;WITH tfr AS (SELECT * FROM WHERE [WholesaleID] = '120-MW')
MERGE INTO [NYP-RetailDataSQL].dbo.[Sales-Comparison0830B]
USING tfr ON md_Retailer = [Cust Id]
WHEN MATCHED THEN UPDATE
SET WholesaleID = WHOLEID;

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
spudmcc

My knowledge of SQL is very very basic.  I really any solution kept very simple to understand and very easy to implement.  

Thanks all!

A
ASKER CERTIFIED SOLUTION
Kevin Cross

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
spudmcc

Yes thanks!

I am now getting the following:

SQL1.sql: Error (6,3): Invalid column name 'WholesaleID'.

The column name is WholesaleID and is dragged right from the column list.  Not sure how this could be invalid.
ASKER
spudmcc

Thank you so much for your patience and knowledge.  Much thanks for your explanation.  it was very helpful and allowed me to finally get this to work.  

Andy
Your help has saved me hundreds of hours of internet surfing.
fblack61
Kevin Cross

No worries. Is WholesaleID a valid column for wholereplace or Sales-Comparison0830B?
If the latter, then the solution is even simpler.

MERGE INTO [NYP-RetailDataSQL].dbo.[Sales-Comparison0830A]
   USING [NYP-RetailDataSQL].dbo.wholereplace
      ON md_Retailer = [Cust Id] AND WholesaleID  = '120-MW'
WHEN MATCHED THEN
   UPDATE
      SET WholesaleID = WHOLEID;
Kevin Cross

It just showed me you accepted the above, so it appears you figured it out. I am glad.

Best regards and happy coding,

Kevin