IF condition THEN
{...statements to execute when condition is TRUE...}
END IF;
IF...THEN...ELSE... Syntax
IF condition THEN
{...statements to execute when condition is TRUE...}
ELSE
{...statements to execute when condition is FALSE...}
END IF;
IF...THEN...ELSEIF... Syntax
IF condition1 THEN
{...statements to execute when condition1 is TRUE...}
ELSIF condition2 THEN
{...statements to execute when condition2 is TRUE...}
END IF;
IF...THEN...ELSEIF...ELSE... Syntax
IF condition1 THEN
{...statements to execute when condition1 is TRUE...}
ELSIF condition2 THEN
{...statements to execute when condition2 is TRUE...}
ELSE
{...statements to execute when both condition1 and condition2 are FALSE...}
END IF;
Table : CUSTOMER
Example 1 : ตัวอย่างการใช้ IF...THEN...ELSE ในรูปแบบที่ง่าย ๆ บน Oracle Store Procedure
CREATE OR REPLACE PROCEDURE GET_CUSTOMER(pType IN VARCHAR2)
AS
BEGIN
IF pType = '1' THEN
FOR CusInfo IN (SELECT * FROM CUSTOMER WHERE COUNTRY_CODE = 'US')
LOOP
DBMS_OUTPUT.PUT_LINE(CusInfo.CUSTOMER_ID || ' ' || CusInfo.NAME || ' '
|| CusInfo.EMAIL || ' ' || CusInfo.COUNTRY_CODE || ' ' || CusInfo.BUDGET || ' ' || CusInfo.USED);
END LOOP;
ELSE
FOR CusInfo IN (SELECT * FROM CUSTOMER WHERE COUNTRY_CODE != 'US')
LOOP
DBMS_OUTPUT.PUT_LINE(CusInfo.CUSTOMER_ID || ' ' || CusInfo.NAME || ' '
|| CusInfo.EMAIL || ' ' || CusInfo.COUNTRY_CODE || ' ' || CusInfo.BUDGET || ' ' || CusInfo.USED);
END LOOP;
END IF;
END;