IF-THEN語(yǔ)句順序可以后跟的ELSE語(yǔ)句,當(dāng)條件為FALSE,執(zhí)行其中的可選序列。
IF-THEN-ELSE語(yǔ)句的語(yǔ)法是:
IF condition THEN S1; ELSE S2; END IF;
其中,S1和S2是語(yǔ)句的不同的序列。在IF-THEN-ELSE語(yǔ)句,當(dāng)測(cè)試條件為T(mén)RUE,則該語(yǔ)句S1被執(zhí)行,當(dāng)測(cè)試條件為FALSE,那么執(zhí)行語(yǔ)句S2。例如:
IF color = red THEN dbms_output.put_line('You have chosen a red car') ELSE dbms_output.put_line('Please choose a color for your car'); END IF;
如果布爾表達(dá)式條件計(jì)算結(jié)果為真,那么代碼if-then塊將被執(zhí)行,否則代碼else塊將被執(zhí)行。
讓我們嘗試一個(gè)完整的例子,來(lái)說(shuō)明這一概念:
DECLARE a number(3) := 100; BEGIN -- check the boolean condition using if statement IF( a < 20 ) THEN -- if condition is true then print the following dbms_output.put_line('a is less than 20 ' ); ELSE dbms_output.put_line('a is not less than 20 ' ); END IF; dbms_output.put_line('value of a is : ' || a); END; /
當(dāng)上述代碼在SQL提示符執(zhí)行時(shí),它產(chǎn)生了以下結(jié)果:
a is not less than 20 value of a is : 100 PL/SQL procedure successfully completed.