我有状态表并存储56个状态和ID,我根据存储在[Cases]表中的ID更新FieldValue表中的值列。我可以用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
答案 0 :(得分:0)
如果state和要更新的值有某种关系(比如我在你的例子中看到的值= state + 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行或者一个返回预期值的函数(里面它将是56行),这是一个更好的aproch,以防需要新的值未来
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
即使在第一种情况下,如果值 - 状态关系发生变化,函数也会更好