2017-04-13 47 views
0

更新我有這樣的查詢不能與case語句

update mst_doc_proc_list set is_display = 0, is_mandatory = 1, is_deleted = case 
    when (is_display = 1 and is_mandatory = 1) then 1 else 0 end 
    where eff_date = '11/4/2017'; 

當我第一次執行剛剛is_displayis_mandatory更新。
我必須執行兩次才能更新查詢中的所有內容。我應該如何在單次執行中運行這些查詢?

+0

您能否詳細說明您正在嘗試做什麼?在執行任何更新之前,什麼是原始值?第一次執行更新後,您希望獲得的預期結果是什麼? – gvenzl

回答

0

is_displayis_mandatory在您的CASE語句中取當前行的值,而不是您的新值。

在您的示例查詢中,您實際上有set is_display = 0這意味着您根本不需要case語句,也可以將其硬編碼爲0

在你真正的查詢中,你使用綁定變量的值爲is_displayis_mandatory?如果你是,那麼使用這些值綁定變量在這兩個點:

update mst_doc_proc_list set is_display = ?, is_mandatory = ?, is_deleted = case 
     when (is_display = ? and is_mandatory = ?) then 1 else 0 end 
     where eff_date = '11/4/2017'; 

另外,我可能把這個邏輯的代碼,而不是SQL。

update mst_doc_proc_list set is_display = ?, is_mandatory = ?, is_deleted = ? 
     where eff_date = '11/4/2017';