快速更新 table

update table in short way

我有状态 table 并存储了 56 个状态和 ID,我正在根据存储在 [Cases] table 中的那些 ID 更新 FieldValue table 中的值列。我可以使用 case 语句来获取结果,我不想重复 case 语句 56 次

Update cv 
set Value = 
        case when 
            c.[state] = 1 then 13
            c.[state] = 2 then 14
            c.[state] = 3 then 15
            .
            .
            .
        End     
from 
    [Cases]  c
join 
    [files] f on f.FileName  = c.Name 
join 
    Recordset CR on CR.RecordId = f.id and RecordId = 3
join 
    FieldValue cv on cv.RecordsetId = cr.Id and cv.FieldId = 6
where 
    c.[State] is not null

如果状态和要更新的值有某种关系(如我在您的示例中看到的值 = 状态 + 12),您可以这样做

update cv
set value = c.[state] +12
from [Cases]  c
join [files] f on f.FileName  = c.Name 
join Recordset CR on CR.RecordId = f.id and RecordId = 3
join FieldValue cv on cv.RecordsetId = cr.Id and cv.FieldId = 6
where c.[State] is not null

如果根本没有关系,你需要写 56 行或者一个函数 returns 期望值(里面有 56 行),这是一个更好的方法,以防万一将来需要新的值

update cv
set value = fn_value_from_state(c.[state])
from [Cases]  c
join [files] f on f.FileName  = c.Name 
join Recordset CR on CR.RecordId = f.id and RecordId = 3
join FieldValue cv on cv.RecordsetId = cr.Id and cv.FieldId = 6
where c.[State] is not null

即使在第一种情况下,如果值-状态关系发生变化,功能也会更好