Equivalent to the IF Else statement in Java
IF Boolean expression then
PL/SQL and SQL statements ;
END IF;
IF Boolean expression then
pl/sql and sql statement ;
else
other statements ;
end IF;
IFBoolean expression Then
PL/SQLand theSQLStatement;
elsifOther Boolean expressions Then
Other Statements;
elsif Other Boolean expressions then
Other Statements ;
ELSE
Other Statements ;
END IF;
hint : elsif cannot be written elseif
Request: Check out the salary of employee number 150th, if its salary is greater than or equal to 10000 print ' salary >= 10000 ';
If between 5000 and 10000, print ' 5000<= salary < 10000 '; Otherwise print ' Salary < 5000 '
Declare v_sal employees.salary%type; beginSelectSalary into V_sal fromEmployeeswhereEmployee_id= -; ifv_sal>=10000Then dbms_output.put_line ('salary>=10000'); elsif v_sal>= theThen dbms_output.put_line ('5000<=salary<10000'); ElseDbms_output.put_line ('salary<5000'); Endif; end;
Case expression
Caseselector
whenexpression1 ThenRESULT1
whenexpression2 Thenresult2
when < Span style= "COLOR: #ff0000" > expressionn then resultn
[ ELSE Span class= "Fontstyle2" > resultn+1]
END < Span style= "COLOR: #ff0000" > ;
Example: request: Check out the salary of employee number 150th, if its salary is greater than or equal to 10000 print ' salary >= 10000 ';
if it is between 5000 and 10000, print ' 5000<= salary &L T 10000 '; Otherwise print ' Salary < '
Declare v_sal employees.salary%type; V_temp VARCHAR2 ( -); BeginSelectSalary into V_sal fromEmployeeswhereEmployee_id= -; V_temp:= CaseTrunc (v_sal/50000) when2 Then'salary>=10000' when1 Then'5000<=salary<10000' Else 'salary<5000'end; Dbms_output.put_line (V_sal||','||v_temp); End;
Requirements: Query the job_id of Employee No. 122th, if its value is ' It_prog ', then print ' grade:a ';
' Ac_mgt ', print ' GRADE B ',
' Ac_account ', print ' GRADE C ';
Otherwise print ' GRADE D '
/sql>Declare2v_job_id VARCHAR2 (Ten); 3V_temp VARCHAR2 (Ten); 4begin5 Selectjob_id into v_job_id fromEmployeeswhereEmployee_id= -; 6v_temp:=7 Casev_job_id when'It_prog'Then'A' 8When'AC_MGT'Then'B' 9When'Ac_account'Then'C' Ten Else 'D' Oneend; ADbms_output.put_line (v_job_id| |','||v_temp); -End
PL/SQL (ii) Process Control statements