توضیحات
در دستور if-then-else قطعه کد زمانی اجرا میشود که موقعیت شرط true باشد و در غیر این صورت دستور دیگری را اجرا میکند.
دستور
دستور if-then-else به صورت زیر نوشته میشود:
| ۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ |
IF condition۱ THEN {...statements to execute when condition۱ is TRUE...}ELSIF condition۲ THEN {...statements to execute when condition۲ is TRUE...}ELSE {...statements to execute when both condition۱ and condition۲ are FALSE...}END IF; |
قسمتهای else و elseif اختیاری هستند.
مثال
مثالی دستور شرطی else-if-then را مشاهده میکنیم:
| ۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ ۱۰ ۱۱ ۱۲ ۱۳ ۱۴ ۱۵ ۱۶ ۱۷ ۱۸ ۱۹ ۲۰ ۲۱ ۲۲ ۲۳ ۲۴ ۲۵ ۲۶ ۲۷ ۲۸ ۲۹ ۳۰ ۳۱ ۳۲ ۳۳ ۳۴ ۳۵ |
CREATE OR REPLACE Function IncomeLevel ( name_in IN varchar۲ ) RETURN varchar۲IS monthly_value number(۶); ILevel varchar۲(۲۰); cursor c۱ is SELECT monthly_income FROM employees WHERE name = name_in;BEGIN open c۱; fetch c۱ into monthly_value; close c۱; IF monthly_value <= ۴۰۰۰ THEN ILevel := \'Low Income\'; ELSIF monthly_value > ۴۰۰۰ and monthly_value <= ۷۰۰۰ THEN ILevel := \'Avg Income\'; ELSIF monthly_value > ۷۰۰۰ and monthly_value <= ۱۵۰۰۰ THEN ILevel := \'Moderate Income\'; ELSE ILevel := \'High Income\'; END IF; RETURN ILevel;END; |