Control structure, PL-SQL Programming

Control Structures

The Control structures are the most important PL/SQL extension to the SQL. Not only does PL/SQL let you manipulate Oracle data, it lets you process the data using iterative, conditional, and sequential flow-of-control statements like IF-THEN-ELSE, WHILE-LOOP, FOR-LOOP, EXIT-WHEN, and GOTO. Together, these statements can handle any situation.

Conditional Control

Frequently, it is necessary to take alternative actions depending on the circumstances. The IF THEN-ELSE    statement executes a sequence of statements conditionally. The IF  clause checks the condition; the THEN clause defines what to do if the condition is true; the ELSE clause defines what to do when the condition is false or null.

Consider the program below, that process a bank transaction. Before permitting you to withdraw $500 from account 3, it makes sure that the account has sufficient funds to cover the withdrawal. If the fund is available, the program debit the account. If not, the program inserts a record into an audit table.

-- available online in file 'examp2'


acct_balance NUMBER(11,2);

acct CONSTANT NUMBER(4) := 3;

debit_amt CONSTANT NUMBER(5,2) := 500.00;


SELECT bal INTO acct_balance FROM accounts

WHERE account_id = acct


IF acct_balance >= debit_amt THEN

UPDATE accounts SET bal = bal - debit_amt

WHERE account_id = acct;



(acct, acct_balance, 'Insufficient funds');

-- insert account, current balance, and message




A sequence of statements that uses query results to select an alternative action is common in database applications. Another common sequence inserts/deletes a row only if an associated entry is found in other table. You can pack these common sequences into a PL/SQL block using conditional logic. This can improve the performance and simplify the integrity checks built into Oracle Forms applications.

Iterative Control

The LOOP statements execute a sequence of statements multiple times. You put the keyword LOOP  before the first statement in the sequence and the keywords END LOOP  after the last statement in the sequence. The example below shows the simplest kind of loop, that repeats a sequence of statements repeatedly:


-- sequence of statements


The FOR-LOOP statement specifies a range of integers, after that execute a sequence of statements once for every integer in the range. For e.g., assume that you are a producer of custom-made cars and that each car has a serial number. To keep the track of which customer buys each car, you might use the FOR loop as shown:

FOR i IN 1..order_qty LOOP

UPDATE sales SET custno = customer_id

WHERE serial_num = serial_num_seq.NEXTVAL;


The WHILE-LOOP statement associates a condition with a series of statements. Before every iteration of the loop, the condition is calculated. When the condition is true, the chain of statements is executed, afterward control resumes at the top of the loop. And if the condition is false or null, the loop is bypassed and control passes to the next statement.

In the example below, you find the first employee who has a salary over $4000 and is higher in the chain of the command than employee 7902:

-- available online in file 'examp3'


salary emp.sal%TYPE;

mgr_num emp.mgr%TYPE;

last_name emp.ename%TYPE;

starting_empno CONSTANT NUMBER(4) := 7902;


SELECT sal, mgr INTO salary, mgr_num FROM emp

WHERE empno = starting_empno;

WHILE salary < 4000 LOOP

SELECT sal, mgr, ename INTO salary, mgr_num, last_name

FROM emp WHERE empno = mgr_num;


INSERT INTO temp VALUES (NULL, salary, last_name);



The EXIT-WHEN statement completes a loop if further processing is not possible or undesirable. When the EXIT statement is encountered, the condition in the WHEN clause is checked. If the condition is true, the loop completes and control passes to the next statement. In the example below, the loop completes when the value of total exceeds 25,000:



total := total + salary;

EXIT WHEN total > 25000; -- exit loop if condition is true


-- control resumes here

Sequential Control

The GOTO statement branch to an unconditionally label. The label, an undeclared identifier enclosed by double angle brackets, should precede an executable statement or a PL/SQL block. If executed, the GOTO statement transfers the control to the labeled statement or block, as shown:

IF rating > 90 THEN

GOTO calc_raise; -- branch to label




IF job_title = 'SALESMAN' THEN -- control resumes here

amount := commission * 0.25;


amount := salary * 0.10;


Posted Date: 10/2/2012 1:46:41 AM | Location : United States

Related Discussions:- Control structure, Assignment Help, Ask Question on Control structure, Get Answer, Expert's Help, Control structure Discussions

Write discussion on Control structure
Your posts are moderated
Related Questions
Write a stored procedure that accepts the post code in which the customer resides as the input parameter. The procedure should then use an explicit cursor to display comprehensive

Grouping and Ungrouping in SQL Example specifying EXAM_MARK in place of COURSE in the main FROM clause. Example: Obtaining C_ER2 from EXAM_MARK SELECT CourseId, CAST

Write a program to implement the inverted file shown in the slides (Simple Index file, LabelID file and Data file).  Use the Avail_List to point at the deleted Label IDs so that th

How do I display usernames for students from a student table, assigning each student a username initials001 (initials is the actual student initials), and if the students initials

Overloading: Similar to packaged subprograms, methods of the same type can be overloaded. That is, you can use similar name for various methods if their formal parameters diff

Seeking a programmer to design a legal document with pre-existing fields that could allow the auto-population of client(s) information (i.e. Name, Account Number, Address etc.) int

Enrolment was split - SQL Example shows how relvars IS_CALLED and IS_ENROLLED_ON can be derived from the original ENROLMENT relvar, using projection in the initial assignment

Many of the reports generated from the system calculate the total dollars in a shopper''s purchases. Follow these steps to create a function named TOT_PURCH_SF that accepts a shopp

Loop Labels Like the PL/SQL blocks, loops can also be labeled. The label, an undeclared identifier enclosed by double angle brackets, should appear at the beginning of the LOOP

Passing Cursor Parameters You use the OPEN statement to pass the parameters to a cursor. Unless you want to accept the default values, each proper parameter in the cursor decl