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
Avatar of awking00
awking00
Flag of United States of America image

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

ASKER

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;
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

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.
Avatar of spudmcc
spudmcc
Flag of United States of America image

ASKER

SQL1.sql: Error (4,1): Incorrect syntax near the keyword 'WHERE'.
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Please show the exact SQL you tried that generated the above error. Paste into a code snippet.
Avatar of spudmcc
spudmcc
Flag of United States of America image

ASKER

;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

Avatar of spudmcc
spudmcc
Flag of United States of America image

ASKER

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
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of spudmcc
spudmcc
Flag of United States of America image

ASKER

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.
Avatar of spudmcc
spudmcc
Flag of United States of America image

ASKER

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
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

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;
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

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

Best regards and happy coding,

Kevin
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo