Sunteți pe pagina 1din 8

University of Wollongong in Dubai

CSCI 235 – Databases


Tutorial 6 – PL/SQL Programming [1]
__________________________________________________________________________________

A PL/SQL block has the following structure.

Declare

Declaration statements

BEGIN

Executable statements

EXCEPTION

Exception-Handling Statements

END;

DECLARATION SECTION

It contains definitions of PL/SQL identifiers such as variables, constant, cursors and so on.

For Example

DECLARE

v_first_name varchar2(35);

v_last_name varchar2(35);

c_counter constant number := 0;

EXECUTABLE SECTION

This section contains executable statements that allow you to manipulate the variables that have been
declared in the declaration section.

For Example

BEGIN

SELECT first_name, last_name

INTO v_first_name, v_last_name

FROM student

WHERE student_id = 123;


DBMC_OUTPUT.PUT_LINE (‘Student name: ‘ || v_first_name || ‘ ‘ || v_last_name);

END;

EXCEPTION-HANDLING SECTION

This section contains statements that are executed when a runtime error occurred within the block.
Runtime errors occur while a program is running and cannot be detected by the PL/SQL compiler.
When a runtime error occurs, control is passed to the exception-handling section of the block. The
error is then evaluated, and a specific exception is raised and executed.

EXCEPTION

WHEN NO_DATA_FOUND THEN

DBMC_OUTPUT.PUT_LINE (‘There is no student with’ || ‘student_id 123’);

END;

Consider combining these examples into a single PL/SQL block.

DECLARE

v_first_name varchar2(35);

v_last_name varchar2(35);

BEGIN

SELECT first_name, last_name

INTO v_first_name, v_last_name

FROM student

WHERE student_id = 123;

DBMC_OUTPUT.PUT_LINE (‘Student name: ‘ || v_first_name || ‘ ‘ || v_last_name);

EXCEPTION

WHEN NO_DATA_FOUND THEN

DBMC_OUTPUT.PUT_LINE (‘There is no student with’ || ‘student_id 123’);

END;

SUBSTITUTION VARIABLES

SQL*PLUS allows a PL/SQL block to receive input information with the help of substitution
variables. Substitution variables cannot be used to output values, because no memory is allocated for
them.
DECLARE

v_student_id NUMBER := &sv_student_id;

v_first_name varchar2(35);

v_last_name varchar2(35);

BEGIN

SELECT first_name, last_name

INTO v_first_name, v_last_name

FROM student

WHERE student_id = v_student_id;

DBMC_OUTPUT.PUT_LINE (‘Student name: ‘ || v_first_name || ‘ ‘ || v_last_name);

EXCEPTION

WHEN NO_DATA_FOUND THEN

DBMC_OUTPUT.PUT_LINE (‘There is no student with’);

END;

CONDITIONAL STATEMENT

Conditional statement has the following structure:

IF CONDITON 1 THEN

STATEMENT 1;

ELSIF CONDITON 2 THEN

STATEMENT 2;

ELSIF CONDITON 3 THEN

STATEMENT 3;

ELSE

STATEMENT N;

END IF;
For Example

DECLARE

v_num NUMBER := &sv_num;

BEGIN

IF v_num < 0 THEN

DBMC_OUTPUT.PUT_LINE (v_num || ‘ is a negative number’);

ELSIF v_num = 0 THEN

DBMC_OUTPUT.PUT_LINE (v_num || ‘ is equal to zero’);

ELSE

DBMC_OUTPUT.PUT_LINE (v_num || ‘ is a positive number’);

END IF;

END;

CASE STATEMENT

A CASE statement has the following structure.

CASE SELECTOR

When expression 1 then statement 1;

When expression 2 then statement 2;

When expression N then statement N;

Eles statement N+1;

END CASE;

TASK 1

Create a Employee table with columns name and salary.


set echo on

set feedback on

create Table Employee1

Name varchar2(30),

Salary number(15)

);

TASK 2

Insert 4 rows to your table.

SQL> insert into Employee1 values('John',3000);

1 row created.

SQL> insert into Employee1 values('Shahid', 5000);

1 row created.

SQL> insert into Employee1 values('Mohd', 8000);

1 row created.

SQL> insert into Employee1 values('Baker', 9000);

1 row created.

TASK 3

Write PL/SQL program to return salary of a certain employee.

SET SERVEROUTPUT ON;

DECLARE

v_name varchar2(30);

v_salary number(15);

BEGIN

SELECT Salary
INTO v_salary

FROM Employee1

WHERE Name='John';

DBMS_OUTPUT.PUT_LINE ('Employee salary:'|| v_name || ' ' || v_salary);

EXCEPTION

WHEN NO_DATA_FOUND THEN

DBMS_OUTPUT.PUT_LINE ('There is no employee with');

END;

TASK 4

Write PL/SQL program to return salary of a given employee. (User Input)

SET SERVEROUTPUT ON;

DECLARE

v_name varchar2(30) :=&v_name;

v_salary number(15);

BEGIN

SELECT Salary

INTO v_salary

FROM Employee1

WHERE Name=v_name;

DBMS_OUTPUT.PUT_LINE ('Employee salary:'|| v_name || ' ' || v_salary);

EXCEPTION

WHEN NO_DATA_FOUND THEN

DBMS_OUTPUT.PUT_LINE ('There is no employee with');

END;
TASK 5

Revise TASK 4 in a way that produces following output:

IF SALARY < 5,000 OUTPUT ‘LOW RANGE’

IF SALARY = 5,000 OUTPUT ‘AVERAGE RANGE’

IF SALARY > 5,000 OUTPUT ‘HIGH RANGE’

SET SERVEROUTPUT ON;

DECLARE

v_name varchar2(30) :=&v_name;

v_salary number(15);

BEGIN

SELECT Salary

INTO v_salary

FROM Employee1

WHERE Name=v_name;

IF v_salary < 5000 THEN

DBMS_OUTPUT.PUT_LINE (v_salary || 'LOW RANGE');

ELSIF v_salary = 5000 THEN

DBMS_OUTPUT.PUT_LINE (v_salary || 'AVERAGE RANGE');

ELSE

DBMS_OUTPUT.PUT_LINE (v_salary || 'HIGH RANGE');

END IF;

END;

S-ar putea să vă placă și