2012-02-16 96 views
1

我需要在一行中運行這個四倍我怎麼能做到這一點,也可以少線我該如何縮短這個SQL?

update opp set run_mode = 0, run_time = 0, 
where oppar_job_name in ('ABC')) and oppar_job_rec in ('ABC'); 

update opp set run_mode = 0, run_time = 0, 
where oppar_job_name in ('DEF')) and oppar_job_rec in ('DEF'); 

update opp set run_mode = 0, run_time = 0, 
where oppar_job_name in ('FGH')) and oppar_job_rec in ('FGH'); 

update opp set run_mode = 0, run_time = 0, 
where oppar_job_name in ('IJK')) and oppar_job_rec in ('IJK'); 

我想嘗試這一點。

update opp set run_mode = 0, run_time = 0, 
where oppar_job_name in (
'ABC', 
'DEF', 
'FGH', 
'IJK' 
) and oppar_job_rec in 
(
'ABC', 
'DEF', 
'FGH', 
'IJK' 
); 

以上是正確的事情。 我的意思是有一一對應的表像

ABC ABC 
DEF DEF 
FGH FGH 
IJK IJK 
+0

你縮短SQL也會影響其ABC DEF,ABC FGH,ABC IJK,行..等 – Jayy 2012-02-16 13:24:45

+0

@ KaipaMSarma沒有,因爲他說這兩列之間有一對一的對應關係。 – Behrang 2012-02-16 13:29:44

回答

6

如果我沒有理解好:

update opp set run_mode = 0, run_time = 0, 
where (oppar_job_name = oppar_job_rec) and 
     (oppar_job_rec in ('ABC','DEF','FGH','IJK')); 

它有額外的括號;-)

+0

+1。聰明的工作。我正在尋找這個。順便說一句,你在圖片微笑:) – Jayy 2012-02-16 13:27:56

+0

我喜歡這個謝謝@ ulundo – munish 2012-02-16 13:30:10

+0

thx球員。是的,我在笑! – unludo 2012-02-16 13:52:27

1

如果更好列之間有一對一的對應關係,那麼你的解決方案是正確的(但有語法錯誤),而且unludo的解決方案更好。

否則你可以把它更加簡潔,避免類似這樣的多個更新語句:

update opp set 
    run_mode = 0, run_time = 0 
where 
    (oppar_job_name in ('ABC') and oppar_job_rec in ('ABC')) OR 
    (oppar_job_name in ('DEF') and oppar_job_rec in ('DEF')) OR 
    (oppar_job_name in ('FGH') and oppar_job_rec in ('FGH')) OR 
    (oppar_job_name in ('IJK') and oppar_job_rec in ('IJK')); 

最後,你甚至都不需要使用in運營商,所以你可以寫這樣的事情:

update opp set 
    run_mode = 0, run_time = 0 
where 
    (oppar_job_name = 'ABC' and oppar_job_rec = 'ABC') OR 
    (oppar_job_name = 'DEF' and oppar_job_rec = 'DEF') OR 
    (oppar_job_name = 'FGH' and oppar_job_rec = 'FGH') OR 
    (oppar_job_name = 'IJK' and oppar_job_rec = 'IJK'); 
1

如果你的oppar_job_name和oppar_job_rec謂詞總是相同的,我更喜歡unludo的答案。

否則,如果該參數可能會有所不同,你可以做這樣的事情:

update opp set run_mode = 0, run_time = 0 
where (oppar_job_name, oppar_job_rec) 
in (('ABCname' , 'ABCrec') 
    , ('DEFname' , 'DEFrec') 
    , ('FGHname' , 'FGHrec') 
    , ('IJKname' , 'IJKrec') 
    ); 
+0

很適合不同參數的情況。謝謝 – munish 2012-02-17 14:21:25