2012-08-08 72 views
3

我想根據表中的另一個字段在我的表中設置一個字段。TSQL條件集

這是我想要的功能:

set result = Win if ((select status from tableY) like '%Won%') 
set result = Loss if ((select status from tableY) like '%lost%') 

這並不編譯...我如何得到正確的功能?

回答

5
set result = case when (select status from tableY) like '%Won%' 
        then 'Win' 
        when (select status from tableY) like '%lost%' 
        then 'Lost' 
       -- If neither win or lose don't change a thing 
        else result 
       end 
1

所以,你試圖更新tableY中的列?

UPDATE tableY SET Result = CASE WHEN status LIKE '%Won%' then 'Win' 
           ELSE 'Loss' 
          END 
WHERE (status LIKE '%Won%' AND COALESCE(Result,'') != 'Win') 
    OR (status LIKE '%Lost%' AND COALESCE(Result,'') != 'Loss')