Help with MERGE

  • TableA

    ID | intTrialID| strLocationName| strLocationCity | strLocationState | srtLocationZip | strLocationCountry

    1 | 1 | University Hospital| Phoenix | Arizona | 11111 |United States

    2 | 1 | Hospital San Joao| Leuven | | | Belgium

    3 | 1 | Research Site| Camperdown | New South Wales | |Australia

    4 | 2 | Medical Center| Nashville | Tennessee | 66666 | United States

    5 | 1 | Research Site| Toronto | | |Canada

    6 | 2 | Monteluce | Barcelona | | | Spain

    7 | 2 | Medical Center| Dallas | Texas | 77777 | United States

    Here I have TableB that I populate on the fly and it stores single intTrialID at a time.

    intTrialID| strLocationName| strLocationCity | strLocationState | srtLocationZip | strLocationCountry

    1 | GSK Site | Glasgow | Lanarkshire | G21 3UW |United Kingdom

    1 | St. Helier NHS | Carshalton | Surrey | SM5 1AA | United Kingdom

    1 | University Hospital| Tucson | Arizona | 22222 | United States

    Basically what I need to do is delete corresponded intTrialID from TableA , then insert into TableA whatever is inTableB

    My question: Is there an anyway I can do it somehow using MERGE statement?

  • MERGE can do that. I'd build you a statement but I'm a little lost on what you're asking.

    Corresponding intTrialID by what? Do you mean delete all TableA.intTrialID = 1 rows if there is an entry in TableB with intTrialID = 1? What matches the two rows to determine:

    1) what should be deleted from TableA

    2) what should be replaced in TableA from TableB

    3) what should be left alone in TableA

    -Eddie

    Eddie Wuerch
    MCM: SQL

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply