0% found this document useful (0 votes)
7 views

PLSQL 6 10

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

PLSQL 6 10

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PL/SQL - Operators

 An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulation. PL/SQL language is rich in built-in operators and provides the following types of
operators −
 Arithmetic operators- Addition, Subtraction, Multiplication, Division
 Relational operators- Less then, Greater Than, etc.
 Comparison operators- Like ,Between, In, IsNull
 Logical operators- AND, OR, NOT
 String operators-

PL/SQL - Conditions
Decision-making structures require that the programmer specify one or more conditions to be evaluated
or tested by the program, along with a statement or statements to be executed if the condition is
determined to be true, and optionally, other statements to be executed if the condition is determined to be
false.
Following is the general form of a typical conditional (i.e., decision making) structure found in most of
the programming languages −

PL/SQL programming language provides following types of decision-making statements.

PL/SQL – Loops
A loop statement allows us to execute a statement or group of statements multiple times and following is
the general form of a loop statement in most of the programming languages –
PL/SQL provides the following types of loop to handle the looping requirements.
DECLARE
i number(1);
j number(1);
BEGIN
<< outer_loop >>
FOR i IN 1..3 LOOP
<< inner_loop >>
FOR j IN 1..3 LOOP
dbms_output.put_line('i is: '|| i || ' and j is: ' || j);
END loop inner_loop;
END loop outer_loop;
END;
/

PL/SQL – Arrays

The PL/SQL programming language provides a data structure called the VARRAY, which can store a
fixed-size sequential collection of elements of the same type. A varray is used to store an ordered
collection of data, however it is often better to think of an array as a collection of variables of the same
type.
All varrays consist of contiguous memory locations. The lowest address corresponds to the first element
and the highest address to the last element.
An array is a part of collection type data and it stands for variable-size arrays. We will study other
collection types in a later chapter 'PL/SQL Collections'.
Each element in a varray has an index associated with it. It also has a maximum size that can be changed
dynamically.

Creating a Varray Type


A varray type is created with the CREATE TYPE statement. You must specify the maximum size and
the type of elements stored in the varray.
The basic syntax for creating a VARRAY type at the schema level is −
CREATE OR REPLACE TYPE varray_type_name IS VARRAY(n) of <element_type>
Where,
 varray_type_name is a valid attribute name,
 n is the number of elements (maximum) in the varray,
 element_type is the data type of the elements of the array.

For example −
TYPE namearray IS VARRAY(5) OF VARCHAR2(10);
Type grades IS VARRAY(5) OF INTEGER;

Example-1
DECLARE
type namesarray IS VARRAY(5) OF VARCHAR2(10);
type grades IS VARRAY(5) OF INTEGER;
names namesarray;
marks grades;
total integer;
BEGIN
names := namesarray('Kavita', 'Pritam', 'Ayan', 'Rishav', 'Aziz');
marks:= grades(98, 97, 78, 87, 92);
total := names.count;
dbms_output.put_line('Total '|| total || ' Students');
FOR i in 1 .. total LOOP
dbms_output.put_line('Student: ' || names(i) || '
Marks: ' || marks(i));
END LOOP;
END;
/
Example-2
Elements of a varray could also be a %ROWTYPE of any database table or %TYPE of any database table
field. The following example illustrates the concept.
DECLARE
CURSOR c_customers is
SELECT name FROM customers;
type c_list is varray (6) of customers.name%type;
name_list c_list := c_list();
counter integer :=0;
BEGIN
FOR n IN c_customers LOOP
counter := counter + 1;
name_list.extend;
name_list(counter) := n.name;
dbms_output.put_line('Customer('||counter ||'):'||name_list(counter));
END LOOP;
END;
/

PL/SQL - Procedures
A subprogram is a program unit/module that performs a particular task. These subprograms are
combined to form larger programs. This is basically called the 'Modular design'. A subprogram can be
invoked by another subprogram or program which is called the calling program.
A subprogram can be created –

 At the schema level


 Inside a package
 Inside a PL/SQL block
At the schema level, subprogram is a standalone subprogram. It is created with the CREATE
PROCEDURE or the CREATE FUNCTION statement. It is stored in the database and can be deleted
with the DROP PROCEDURE or DROP FUNCTION statement.
A subprogram created inside a package is a packaged subprogram. It is stored in the database and can be
deleted only when the package is deleted with the DROP PACKAGE statement. We will discuss
packages in the chapter 'PL/SQL - Packages'.
PL/SQL subprograms are named PL/SQL blocks that can be invoked with a set of parameters. PL/SQL
provides two kinds of subprograms −
 Functions − These subprograms return a single value; mainly used to compute and return a value.
 Procedures − These subprograms do not return a value directly; mainly used to perform an
action.

Parts of a PL/SQL Subprogram


Declarative Part
It is an optional part. However, the declarative part for a subprogram does not start with the DECLARE
keyword. It contains declarations of types, cursors, constants, variables, exceptions, and nested
subprograms. These items are local to the subprogram and cease to exist when the subprogram completes
execution.
Executable Part
This is a mandatory part and contains statements that perform the designated action.
Exception-handling
This is again an optional part. It contains the code that handles run-time errors.

Creating a Procedure
A procedure is created with the CREATE OR REPLACE PROCEDUREstatement. The simplified
syntax for the CREATE OR REPLACE PROCEDURE statement is as follows −
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
BEGIN
< procedure_body >
END procedure_name;

Where,
 procedure-name specifies the name of the procedure.
 [OR REPLACE] option allows the modification of an existing procedure.
 The optional parameter list contains name, mode and types of the parameters. IN represents the
value that will be passed from outside and OUT represents the parameter that will be used to
return a value outside of the procedure.
 procedure-body contains the executable part.
 The AS keyword is used instead of the IS keyword for creating a standalone procedure.

Example
The following example creates a simple procedure that displays the string 'Hello World!' on the screen
when executed.
CREATE OR REPLACE PROCEDURE greetings
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/
When the above code is executed using the SQL prompt, it will produce the following result −
Procedure created.

Executing a Standalone Procedure


A standalone procedure can be called in two ways −
 Using the EXECUTE keyword
 Calling the name of the procedure from a PL/SQL block
The above procedure named 'greetings' can be called with the EXECUTE keyword as −
EXECUTE greetings;
The above call will display −
Hello World

PL/SQL procedure successfully completed.


The procedure can also be called from another PL/SQL block −
BEGIN
greetings;
END;
/

You might also like