2013-05-26 24 views
0

我想創建一個包含一系列要檢查的條件的MySQL存儲過程。在MySQL存儲過程中轉到

IF (a && b) then 
    IF c =1 then 
    IF d < e then 
     check conditions 1,2,3... 
    END IF 
    ELSE 
    check conditions 1,2,3... 
    END IF 
END IF 

我想要寫的程序在這樣一種方式,條件1,2,3應寫爲常見,這樣的程序會像

IF (a && b) then 
IF c =1 then 
    IF d < e then 
    goto label check; 
    END IF 
ELSE 
    goto label check; 
END IF 
END IF 

label:label check; 
    check conditions 1,2,3... 

由於goto語句中不存在MySQL存儲程序,我該如何編寫程序?

回答

0

試試這個:

IF a AND b THEN 
    IF c = 1 AND d < e OR c != 1 then 
    check conditions 1,2,3... 
    END IF 
END IF 
0

我建議不要使用goto,即使可用(*)。

您的代碼可以簡化爲:

IF a AND b AND (c != 1 OR d < e) THEN 
    check conditions 
END IF 

此外,混合litteral(ANDOR)布爾運營商和它們的符號同行(&&||)使你的代碼看起來很奇怪(但是這僅僅是我的意見)。

只是爲了避免口水戰,是有時有點goto語句並不壞,但是這是一個goto濫用的一個很好的例子