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

Assignment Help:

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.


Related Discussions:- Pl/sql conditional control: if statements

Select into statement - syntax, SELECT INTO Statement   The SELECT INT...

SELECT INTO Statement   The SELECT INTO statement retrieve data from one or more database tables, and then assigns the selected values to the variables or fields. Syntax:

Implicit cursors, Implicit Cursors The Oracle implicitly opens a curso...

Implicit Cursors The Oracle implicitly opens a cursor to process each SQL statement not related with an explicitly declared cursor. The PL/SQL lets you refer to the most recen

Advantages of wrapping, Advantages of Wrapping   The PL/SQL Wrapper co...

Advantages of Wrapping   The PL/SQL Wrapper convert the PL/SQL source code into a transitional form of the object code. By hiding the application internals, the Wrapper secure

Write a program to implement inverted file shown slider, Write a program to...

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

Read-only operator (+) - sql, Read-Only Operator (+) - SQL The term r...

Read-Only Operator (+) - SQL The term read-only operator to the mathematical term function. Here I just need to add that the SQL standard reserves the term function for read-

Semijoin and composition - sql, Semijoin and Composition - SQL For sem...

Semijoin and Composition - SQL For semijoin, the dyadic relational operator MATCHING, defined thus: r1 MATCHING r2, where r1 and r2 are relations such that r1 JOIN r2 is de

Primary key - sql, Primary Key - SQL A PRIMARY KEY specification carri...

Primary Key - SQL A PRIMARY KEY specification carries an implicit NOT NULL constraint on each column of the specified key. When more than one key constraint is required, the k

Theory of eternity of life - origin of life, THEO R Y OF ETERNITY OF LIFE...

THEO R Y OF ETERNITY OF LIFE (PRAYER - 1880) - The theory of eternity of life, also called the steady-state theory , states that life has ever been in existence as at presen

Types of evolution, TYPES OF EVOLUTION - Sequential evolution         ...

TYPES OF EVOLUTION - Sequential evolution                  :                    Minor changes in the gene pool of a population from one generation to the next, with the resul

Magento change address format depending on store, Magento change address fo...

Magento change address format depending on store Project Description: What I need is that depending on the store in which the customer bought the address should change the fo

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd