Pl/sql conditional control: if statements, PL-SQL Programming

Pl/sql Conditional Control: IF statements

Frequently, it is necessary to take the alternative actions depending on the circumstances. The IF statement execute a series of statements conditionally. That is, whether the series is executed or not depends on the value of the condition. There are 3 forms of IF statements: IF-THEN, IF-THEN-ELSE, & IF-THEN-ELSIF.


IF-THEN

The simplest form of the IF statement acquaintances a condition with a series of statements enclosed by the keywords THEN and END IF (not ENDIF), as shown below:


IF condition THEN
sequence_of_statements
END IF;

The series of statements is executed only if the condition is true. When the condition is false or null, then the IF statement can do nothing. In either of the case, the control passes to the next statement. An illustration is shown below:


IF sales > quota THEN
compute_bonus(empid);
UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id;
END IF;

You may want to place brief IF statements on a single line, as in

IF x > y THEN high := x; END IF;



IF-THEN-ELSE

The IF statement that is the second form adds the keyword ELSE follow by an alternative series of statements is as shown below:


IF condition THEN
sequence_of_statements1
ELSE
sequence_of_statements2
END IF;


The series of statements in the ELSE clause is executed only if the condition is false or null. Therefore, the ELSE clause ensure that a sequence of statements is executed. In the example below, the first UPDATE statement is executed if the condition is true, while  the second UPDATE statement is executed if the condition is false or null:


IF trans_type = ’CR’ THEN
UPDATE accounts SET balance = balance + credit WHERE...
ELSE
UPDATE accounts SET balance = balance - debit WHERE...
END IF;


The THEN and ELSE clauses can involve the IF statements. That is, the IF statements can be nested, as the example below shows:

IF trans_type = ’CR’ THEN
UPDATE accounts SET balance = balance + credit WHERE...
ELSE
IF new_balance >= minimum_balance THEN
UPDATE accounts SET balance = balance - debit WHERE ...
ELSE
RAISE insufficient_funds;
END IF;
END IF;


IF-THEN-ELSIF

At many times you want to select an action from some mutually exclusive alternatives. The third form of the IF statement uses the keyword ELSIF to introduce the additional conditions which is as shown below:

IF condition1 THEN
sequence_of_statements1
ELSIF condition2 THEN
sequence_of_statements2
ELSE
sequence_of_statements3
END IF;


When the first condition is false or null, then the ELSIF clause tests another condition. An IF statement can have a few number of ELSIF clauses; the final ELSE clause is elective. The Conditions are evaluated one by one from top to bottom. When any condition is true, its related sequence of statements is executed and the control passes to the next statement. If all the conditions are false or null, then the sequence in the ELSE clause is executed. Consider the following illustration as shown below:


BEGIN
...
IF sales > 50000 THEN
bonus := 1500;
ELSIF sales > 35000 THEN
bonus := 500;
ELSE
bonus := 100;
END IF;
INSERT INTO payroll VALUES (emp_id, bonus, ...);
END;


When the value of sales is bigger than 50000, the first and second conditions are true.
However, bonus is assigned the proper value of 1500 as the second condition is never tested. When the first condition is true, its related statement is executed and the control passes to the INSERT statement.

Posted Date: 10/3/2012 5:42:21 AM | Location : United States







Related Discussions:- Pl/sql conditional control: if statements, Assignment Help, Ask Question on Pl/sql conditional control: if statements, Get Answer, Expert's Help, Pl/sql conditional control: if statements Discussions

Write discussion on Pl/sql conditional control: if statements
Your posts are moderated
Related Questions
How Exceptions Propagate ? Whenever an exception is raised, and if the PL/SQL cannot find a handler for it in the present subprogram or block, the exception propagates. That is

Use the RETURNING Clause Frequently, the application requires information about the row affected by a SQL operation, for illustration, to produce a report or take a subsequent

NULL Statement The NULL statement clearly specifies in action; it does nothing other than to pass control to the next statement. It can, though, improve the readability. In a

Use Serially Reusable Packages To help you to manage the use of memory, the PL/SQL gives the pragma SERIALLY_ REUSABLE that mark some packages as serially reusable . So mark

Packages The package is a schema object which groups logically associated to the PL/SQL items, types, and subprograms. The Packages have 2 sections: the specification & the bod

Using the FORALL Statement The keyword FORALL instruct the PL/SQL engine to bulk-bind input collections before sending them all to the SQL engine. Though the FORALL statement

SQL Cursor   The Oracle implicitly opens a cursor to process each SQL statement not related with an explicit cursor. The PL/SQL refers to the most current implicit cursor as t

1. Create a procedure called TAX_COST_SP to accomplish the tax calculation task. Keep in mind that the state and subtotal values are inputs into the procedure and the procedure is

3CX PBX sync Windows Service Project Description: !! You require access to a commercial version of the 3CX PBX system in order to be able to program the API !! !! You requ

Using Host Arrays The Client-side programs can use anonymous PL/SQL blocks to bulk-bind input and output host arrays. However, this is the well-organized way to pass the colle