在两个表上使用JOIN的条件UPDATE语句

时间:2019-07-03 00:04:13

标签: sql-server

我正在尝试有条件地更新连接到合同表的结构表,其中需要更新不同的许可类型。我以2为例,但有10个要更新:

update structure set
  structure_item_cost = case when (contract_lut_contract_status_id in 
      (17,16) and structure_lut_permit_type_id = 6) then 2500
    else
    case when (contract_lut_contract_status_id in (17,16) and 
      structure_lut_permit_type_id = 17) then 5000
    end
FROM structure
join contract on contract_interest_id = structure_interest_id

我希望根据两个条件更新结构物料成本: contract_lut_contract_status_id in (17,16) 然后在不同条件下where structure_lut_permit_type_id = 17

1 个答案:

答案 0 :(得分:0)

只需删除else情况

update structure
set structure_item_cost = case 
                              when (contract_lut_contract_status_id in 
(17,16) and structure_lut_permit_type_id = 6) then 2500
                              when (contract_lut_contract_status_id in (17,16) and 
structure_lut_permit_type_id = 17) then 5000 end
FROM structure join contract on contract_interest_id = structure_interest_id

或在案例陈述中添加另一端

update structure
set structure_item_cost = case when (contract_lut_contract_status_id in 
  (17,16) and structure_lut_permit_type_id = 6) then 2500
else
case when (contract_lut_contract_status_id in (17,16) and 
  structure_lut_permit_type_id = 17) then 5000
end end
FROM structure join contract on contract_interest_id = structure_interest_id