Question

So a senior in my team is updating staging_table with the corresponding values in tb_party table. I could understand if he used an inner join (in which case, the staging_table would be updated for exact matches). But why is he using right outer join? Can somebody please explain the significance of right join in below query? What will happen to staging table if there is no match for certain records?

UPDATE C
    SET c.party_first_name = e.customername
        ,c.party_type = 
        CASE 
        WHEN e.ClassificationType is not null THEN e.ClassificationType
        ELSE c.party_type
        END
        ,c.tax_indicator = 
        case
        when c.party_type='entity' then e.TaxNonTaxGovernmentIDNumberEntitiesTaxNonTaxGovernmentIDType
        Else NULL
        END
    FROM staging_table C
        right join MTB_AML.dbo.tb_party E
        on c.party_key = E.CustomerInternalID
Was it helpful?

Solution

He joins the E table to set the values in `C' to values based on the different updates. Let's break it down.

c.party_first_name = e.customername

Here, if there is no match, the party_first_name will be set to NULL

c.party_type = 
    CASE 
    WHEN e.ClassificationType is not null THEN e.ClassificationType
    ELSE c.party_type
    END

Here, if there is no match, the party_type will stay whatever it is set in C, with no change. If there is a match, then it will set party_type to e.classificationType

c.tax_indicator = 
    case
    when c.party_type='entity' then e.TaxNonTaxGovernmentIDNumberEntitiesTaxNonTaxGovernmentIDType
    Else NULL
    END

Here, if the party_type is equal to entity then it will take whatever value is in e.TaxNonTaxGovernmentIDNumberEntitiesTaxNonTaxGovernmentIDType regardless of a match or not. If the party_type does not equal entity then it will be set to NULL

OTHER TIPS

"The RIGHT JOIN keyword returns all rows from the right table (MTB_AML.dbo.tb_party), with the matching rows in the left table (staging_table). The result is NULL in the left side when there is no match."

Which for you means that the table will be joined and any data that is in MTB_AML.dbo.tb_party and doesn't match staging_table that is required for your update statement that isn't covered by your case statement will be displayed as null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top