0% found this document useful (0 votes)
72 views21 pages

PL/SQL: Submitted By: Nabanita Sarkar

PL/SQL is a programming language extension of SQL. It allows developers to handle errors and communicate with Oracle database objects. PL/SQL code is written in a block structure with optional declare, mandatory begin and exception sections. It supports data types like numbers, dates, and strings. PL/SQL includes control structures like loops and conditionals to control program flow. Developers can create procedures and functions to modularize code. Cursors allow processing of query results one row at a time. Packages are used to organize related PL/SQL objects.

Uploaded by

Nabanita Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views21 pages

PL/SQL: Submitted By: Nabanita Sarkar

PL/SQL is a programming language extension of SQL. It allows developers to handle errors and communicate with Oracle database objects. PL/SQL code is written in a block structure with optional declare, mandatory begin and exception sections. It supports data types like numbers, dates, and strings. PL/SQL includes control structures like loops and conditionals to control program flow. Developers can create procedures and functions to modularize code. Cursors allow processing of query results one row at a time. Packages are used to organize related PL/SQL objects.

Uploaded by

Nabanita Sarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

PL/SQL

Submitted by : Nabanita Sarkar


Introduction

 PL/SQL stands for procedural language extension to SQL.


 it helps to handle the advance error.
 Communicates natively with other oracle database objects.
 PL/SQL provides a block structure for executable units of code.
PL/SQL block structure
 Declare ( optional) : where we declare the variables.
 BEGIN ( mandatory) : SQL statements or PL/SQL statements
 EXCEPTION ( optional) : actions to perform when errors occur.
 END (mandatory) ;

DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
PL/SQL - Data Types
 CHAR ( MAX_LENGTH)
 VARCHAR2 ( MAX LENGTH)
 NUMBER (PRECISION, SCALE)
 BINARY_INTEGER
 RAW
 DATE
 BOOLEAN
PL/SQL VARIABLES

 Variables are declared and initialized in the declarative section of the


code block.
 It used and assigned new values in the executable section.
 Passed as parameters to PL/SQL .
 VARIABLE SCOPES ARE 2 TYPES :
1. Local variables − Variables declared in an inner block and not accessible
to outer blocks.
2. Global variables − Variables declared in the outermost block or a package
PL/SQL LOOP

 A loop statement allows us to execute a statement or group of statements


multiple times ( PARTICULAR TIMES )
 LOOP TYPES AND DECRIPTION : There are mainly 3 types of LOOP.
1. BASIC LOOP : A loop statement allows us to execute a statement or group of
statements multiple times.
2. WHILE LOOP : Repeats a statement or group of statements while a given
condition is true. It tests the condition before executing the loop body.
3. FOR LOOP : Execute a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
BASIC LOOP STRUCTURE & EXAMPLE :

 EXAM PLE
LOOP DECLARE
Sequence of statements;
x number := 10;
END LOOP;
BEGIN LOOP
dbms_output.put_line(x);
x := x + 10;
IF x > 50 THEN exit;
END IF;
END LOOP;
dbms_output.put_line
('After Exit x is: ' || x);
END;
WHILE LOOP STRUCTURE & EXAMPLE :

WHILE condition LOOP


sequence_of_statements
END LOOP;
DECLARE
a number(2) := 10; BEGIN
WHILE a < 20 LOOP
dbms_output.put_line('value of a: ' || a);

a := a + 1;
END LOOP;
END;
FOR LOOP STRUCTURE & EXAMPLE :

FOR counter IN
initial_value .. final_value DECLARE
LOOP a number(2);
sequence_of_statements;
END LOOP; BEGIN
FOR a in 10 .. 20
LOOP

dbms_output.put_line('value of a: ' || a);

END LOOP;
END;
PROCEDURES

 A subprogram is a program unit/module that performs a particular task.


These subprograms do not return a value directly; mainly used to perform
an action.
 CREATING PROCEDURES : A procedure is created with the CREATE OR
REPLACE PROCEDURE statement.

CREATE [OR REPLACE] PROCEDURE procedure_name


[(parameter_name [IN | OUT | IN OUT] type [, ...])] {IS | AS}
BEGIN
< procedure_body >
END procedure_name;
PARAMETER MODE :
 IN : An IN parameter lets you pass a value to the subprogram. It is a read-
only parameter. Inside the subprogram, an IN parameter acts like a
constant. It is the default mode of parameter passing. Parameters are
passed by reference.

 OUT : An OUT parameter returns a value to the calling program. Inside the
subprogram, an OUT parameter acts like a variable. The actual parameter
must be a variable and it is passed by a value.

 IN OUT : An IN OUT parameter passes an initial value to a subprogram


and returns an updated value to the caller. It can be assigned a value and
the value can be read. Actual parameter is passed by a value.
Execute standalone program :

 CREATING PROCEDURE :
CREATE OR REPLACE PROCEDURE PROCEDURE_NAME
AS
BEGIN
dbms_output.put_line(‘STATEMENTS’)
END;
 EXECUTE : EXECUTE PROCEDUR
 ; DROP : DROP PROCEDURE PROCEDURE_NAME .
FUNCTION
 Functions − These subprograms return a single value; mainly used to
compute and return a value.
 Creating a Function : A standalone function is created using the CREATE
FUNCTION statement. The basic syntax of creating a function is :

CREATE [OR REPLACE] FUNCTION function_name


[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];
CURSOR

 The oracle engine uses a work area for its internal processing in order to
execute an SQL statement. This work is private to SQL operations and is
called a cursor. The set of rows the cursor holds is referred to as the active
set.
 There are 2 types of cursor.
1. Implicit cursor
2. Explicit cursor
Implicit cursor and attributes
 Implicit cursor is automatically created when a query or manipulation is for
a single row.
Attributes Description

%FOUND Returns TRUE if an INSERT,


UPDATE, or DELETE statement
affected one or more rows

%NOTFOUND The logical opposite of


%FOUND. It returns TRUE if an
INSERT, UPDATE, or DELETE
statement affected no rows

%ROWCOUNT Returns the number of rows


affected by an INSERT,
UPDATE, or DELETE statement
Example on Implicit Cursor :

DECLARE
total_rows number(2);
BEGIN
UPDATE table_name
statements
IF sql%notfound THEN
dbms_output.put_line(‘statements');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' statement ');
END IF;
END;
Explicit cursor

 Explicit cursors are programmer-defined cursors for gaining more control


over the context area. An explicit cursor should be defined in the
declaration section of the PL/SQL Block.
 The syntax for creating an explicit cursor is −
CURSOR cursor_name IS select_statement;
Working with an explicit cursor includes the following steps −
 Declaring the cursor for initializing the memory
 Opening the cursor for allocating the memory
 Fetching the cursor for retrieving the data
 Closing the cursor to release the allocated memory
PL/SQL PACKAGES

 PL/SQL PACKAGES logically related components :


• PL/SQL types
• variables , data structures and exceptions
• Subprograms : procedure and functions
 PL/SQL PACKAGE consist of two parts :
 Specification
 A body
Package Specification :

 The specification is the interface to the package. It just DECLARES the types,
variables, constants, exceptions, cursors, and subprograms that can be
referenced from outside the package.
 Syntax to create a package :
CREATE OR REPLACE PACKAGE package_name AS
PROCEDURE procedure_name (statement);
END package_name;
PACKAGE BODY:
 The package body has the codes for various methods declared in the
package specification and other private declarations, which are hidden
from the code outside the package.
 he basic syntax of package body is :

CREATE OR REPLACE package package_name


as/is
Variable declaration
Subprogram bodies

BEGIN
Initialization statements
End package_name;
GUIDELINES FOR WRITING PACKAGES :

 Define the package specification before the body.


 The package specification should contain only those constructs that we
want to be public.
 Place items in the declaration part of the package body.

THANK YOU

You might also like