UPDATE语句不适用于SQL Server中的某些列

时间:2014-05-08 20:36:13

标签: sql sql-server sql-update case operator-keyword

我在存储过程中有一个更新语句。没有更新任何记录。因此,我将更新声明分为两部分,如下所述 -

第一次更新成功运行,但第二次更新未更新具有相同连接条件且匹配的任何tax_id记录。我不确定问题是否与CASE语句中的IN运算符用法有关。有人可以帮忙吗?

UPDATE s
    SET s.is_updated = '1'
        ,s.update_dt = GETDATE()
        ,s.update_source = 'ECM'
        ,s.party_type = 
        CASE 
        WHEN e.ClassificationType is not null THEN e.ClassificationType
        ELSE s.party_type
        END
    FROM staging_cust_acct s 
        right join MTB_AML.dbo.tb_party_kyc e 
        on s.party_key = e.CustomerInternalID





UPDATE s
SET      s.is_updated = '1'
    ,s.update_dt = GETDATE()
    ,s.update_source = 'ECM'
    ,s.tax_id = 
    case 
    when s.party_type IN ('Individual-Retail','Individual-High Net Worth') then e.TaxIndividualID
    when s.party_type IN ('Corp-Publicly Traded','FI-MSB'
                        ,'Corp-NFP/NGO','Corp-Personal/Non-Operating (WHV)'
                        ,'Corp-Not Publicly Traded/Operating') 
         then e.TaxEntitiesID
    when s.party_type = 'Government' then null
    else s.tax_id end
FROM staging_cust_acct s 
    right join MTB_AML.dbo.tb_party_kyc e 
    on s.party_key = e.CustomerInternalID

1 个答案:

答案 0 :(得分:0)

我试图使用RIGHT join更新连接左侧的表格。将连接转换为LEFT OUTER JOIN工作正常。故事的道德 - 永远不要使用正确的加入:)

UPDATE s
SET      s.is_updated = '1'
    ,s.update_dt = GETDATE()
    ,s.update_source = 'ECM'
    ,s.tax_id = 
    case 
    when s.party_type IN ('Individual-Retail','Individual-High Net Worth') then e.TaxIndividualID
    when s.party_type IN ('Corp-Publicly Traded','FI-MSB'
                        ,'Corp-NFP/NGO','Corp-Personal/Non-Operating (WHV)'
                        ,'Corp-Not Publicly Traded/Operating') 
         then e.TaxEntitiesID
    when s.party_type = 'Government' then null
    else s.tax_id end
FROM staging_cust_acct s 
    left outer join MTB_AML.dbo.tb_party_kyc e 
    on s.party_key = e.CustomerInternalID