2015-08-28 114 views
1

我有狀態表和存儲56個狀態和ID,並且我正在基於存儲在[Cases]表中的那些ID更新FieldValue表中的值列。我可以用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 

回答

0

如果狀態和更新值有某種關係的(就像值=狀態+ 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線長的函數),這是在未來需要新的價值的情況下更好的支持

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 

即使在第一種情況下,如果價值狀態關係發生變化,功能也會更好