SlideShare a Scribd company logo
LOGO
PL/SQL
LOGO
PL/SQL
Introduction to PL/SQL
Data types in PL/SQL
Control Structures in PL/SQL
Functions in PL/SQL
LOGO
PL/SQL
Procedure
Cursor
Trigger
Exception In PL/SQL
PL/SQL
 PL/SQL is a combination of SQL along with the
procedural features of programming languages.
 Properties of programming as well as the great
interaction with database.
Points to Ponder:-
PLSQL IS NOT A CASE SENSITIVE LANGUAGE.
LOGO
Sub titleIntroduction to PL/SQL
PL/SQL
Points to be Ponder:-
LOGO
Sub titleComments in PL/SQL
PL/SQL
 DECLARE :- if you want to declare a variable in
plsql program then it takes place in declare
section.
 BEGIN:- is used to start the working of program.
 END:- is used to terminate the begin.
Points to Ponder:-
DELIMITER IS USED TO RUN (/)
LOGO
Sub titleImportant PL/SQL Concepts
PL/SQL
 SET SERVEROUTPUT ON :- is run after every time
when you login in a session.
LOGO
Sub titleWhat to do Previously
PL/SQL
DBMS_OUTPUT.PUT_LINE command
for e.g. if sal=10 and you want to print it.
Then it looks like
dbms_output.put_line(‘the salary is ‘ ||sal);
LOGO
Sub titleTo show OUTPUT on Your Screen
PL/SQL
Declare
Num number(11);
Begin
Num:=5;
End;
/
LOGO
Sub titleValue assign in Variable
PL/SQL
DECLARE
N NUMBER(11);
BEGIN
DBMS_OUTPUT.PUT_LINE('ENTER A NUMBER:-');
N:=&N;
DBMS_OUTPUT.PUT_LINE('THE VALUE IS'||N);
END;
/
LOGO
Sub titleUser’s Input for a Variable
PL/SQL
BEGIN
DBMS_OUTPUT.PUT_LINE(‘Hello World');
END;
/
LOGO
Sub titleSample program to print your ‘Hello World’
PL/SQL
Syntax:-
IF condition THEN
statement 1;
ELSE
statement 2;
END IF;
Points to Ponder:-
IF STATEMENT WORKS AS SIMILAR AS C OR C++
LOGO
Sub titleControl Structures in PL/SQL
IF STATEMENT
PL/SQL
DECLARE
Age number(11);
Begin
Age:=&age;
If age>18 then
Dbms_output.put_line(‘You can vote’);
Else
Dbms_output.put_line(‘You cannot vote’);
End if;
End;
/
LOGO
Sub titleControl Structures in PL/SQL
IF STATEMENT EXAMPLE
PL/SQL
Declare
A number(11);
Begin
Select sal into a from empcp where
ename='MARTIN';
If a>1000 then
Update empcp set comm=comm+1000 where
ename='MARTIN';
Else
Update empcp set comm=comm+500 where
ename='MARTIN';End if;End;/
LOGO
Sub titleControl Structures in PL/SQL
IF WITH SQL TABLE
PL/SQL
1)SIMPLE LOOP
2)WHILE LOOP
3)FOR LOOP
LOGO
Sub titleControl Structures in PL/SQL
LOOPS IN PL/SQL
PL/SQL
Print number from 1 to 10 using FOR loop
BEGIN
FOR i in [REVERSE] 1 ..10 Loop
Dbms_output.put_line(i);
END Loop;
End;
/
Points to Ponder:-
(For FOR LOOP has NO need to initialize variable i
explicitly but it need in while )
LOGO
Sub titleControl Structures in PL/SQL
FOR LOOP
PL/SQL
Print number from 1 to 10 using WHILE loop
Declare
i number(3):=0;
Begin
While i<10 loop
i:=i+1;
Dbms_output.put_line(i);
End loop;
End;
/
LOGO
Sub titleControl Structures in PL/SQL
WHILE LOOP
PL/SQL
LOOP
Statement 1;
Statement 2;
Exit condition
End loop;
LOGO
Sub titleControl Structures in PL/SQL
SYNTAX OF SIMPLE LOOP
PL/SQL
Declare
I number(2):=0;
Begin
Loop
dbms_output.put_line('Before Increment value of
variable I: ' || I);
I:=I+1;
dbms_output.put_line('After Increment value of
variable I: ' || I);
EXIT when (I>0);END loop;END;/
LOGO
Sub titleControl Structures in PL/SQL
SIMPLE LOOP EXAMPLE
PL/SQL
CASE (expression)
WHEN <value1> THEN action_blockl;
WHEN <value2> THEN action_block2;
WHEN <value3> THEN action_block3;
ELSE action_block_default;
END CASE;
LOGO
Sub titleControl Structures in PL/SQL
SYNTAX OF “CASE”
PL/SQL
Declare
a NUMBER :=55;
b NUMBER :=5;
arth_operation VARCHAR2(20) :='MULTIPLY';
BEGIN
dbms_output.put_line('Program started.' );
CASE (arth_operation)
WHEN 'ADD' THEN dbms_output.put_line('Addition of the numbers are:' || a+b );
WHEN 'SUBTRACT' THEN dbms_output.put_line('Subtraction of the numbers are: '||a-b );
WHEN 'MULTIPLY' THEN dbms_output.put_line('Multiplication of the numbers are: '|| a*b
);
WHEN 'DIVIDE' THEN dbms_output.put_line('Division of the numbers are:'|| a/b);
ELSE dbms_output.put_line('No operation action defined. Invalid operation');
END CASE;
dbms_output.put_line('Program completed.' );
END;
/
LOGO
Sub titleControl Structures in PL/SQL
EXAMPLE OF “CASE”
PL/SQL
 This is similar to passing parameters in
programming languages.
 We can pass values to the stored procedure and
function through these parameters or variables.
 This type of parameter is a read only parameter.
Points to Ponder:-
We can assign the value of IN type parameter to
a variable or use it in a query, but we cannot
change its value inside the procedure.
LOGO
Sub titleFunctions in PL/SQL
“IN” PARAMETER
PL/SQL
CREATE [OR REPLACE] FUNCTION function_name
(param_name1 IN data_type, param_name2 IN
data_type.. )
Here:-
(i)param_name1, param_name2... are unique parameter
names.
(ii) data_type - defines the DataType of the variable.
(iii) IN - is optional, by default it is a IN type parameter.
LOGO
Sub titleFunctions in PL/SQL
“IN” PARAMETER SYNTAX
PL/SQL
 The OUT parameters are used to send the OUTPUT
from a procedure or a function.
 This is a write-only parameter i.e. we cannot pass
values to OUT parameter while executing the
stored procedure, but we can assign values to
OUT parameter inside the stored procedure and
the calling program can receive this output value.
LOGO
Sub titleFunctions in PL/SQL
“OUT” PARAMETER
PL/SQL
CREATE [OR REPLACE] PROCEDURE
procedure_name
(param_name1 IN data_type, param_name2 IN
data_type , param_name3 OUT data_type)
Here:-
(i) param_name1, param_name2 are unique parameter
Names and can not be modified with in procedure.
(ii) param_name3 can be modified with in procedure.
(iii) data_type - defines the DataType of the variable.
LOGO
Sub titleFunctions in PL/SQL
“OUT” PARAMETER SYNTAX
PL/SQL
CREATE OR REPLACE PROCEDURE procname_outparam (inparam1 in VARCHAR2,outParam2
OUT VARCHAR2)
IS
BEGIN
outParam2 := 'Hello World OUT parameter I am changeable parameter' || inparam1;
END;
/
Run it:-
DECLARE
outParam2 VARCHAR2(100);
inparam1 VARCHAR2(100);
BEGIN
Procname_outparam ('i am IN parameter',outParam2);
DBMS_OUTPUT.PUT_LINE(outParam2);
END;
/
LOGO
Sub titleFunctions in PL/SQL
EXAMPLE ON “IN” AND “OUT”
PL/SQL
 The IN OUT parameter allows us to pass values
into a procedure and get output values from the
procedure.
 This parameter is used if the value of the IN
parameter can be changed in the calling program.
 By using IN OUT parameter we can pass values
into a parameter and return a value to the calling
program using the same parameter. But this is
possible only if the value passed to the procedure
and output value have a same data type.
LOGO
Sub titleFunctions in PL/SQL
“INOUT” PARAMETER
PL/SQL
CREATE [OR REPLACE] PROCEDURE
procedure_name
(param_name IN OUT data_type)
Here:-
(i) param_name are unique parameter
Names and can not be modified with in procedure.
(ii) param_name3 can be modified with in procedure.
(iii) data_type - defines the DataType of the variable.
LOGO
Sub titleFunctions in PL/SQL
“IN OUT” PARAMETER SYNTAX
PL/SQL
Functions is a standalone PL/SQL subprogram.
Like PL/SQL procedure, functions have a unique
name by which it can be referred. These are
stored as PL/SQL database objects. Below are
some of the characteristics of functions.
 Functions are a standalone block that is mainly
used for calculation purpose.
 Function use RETURN keyword to return the
value, and the data type of this is defined at the
time of creation.
LOGO
Sub titleFunctions in PL/SQL
DEFINITION OF FUNCTION
PL/SQL
 A Function should either return a value or raise
the exception, i.e. return is mandatory in
functions.
Points to Ponder:-
* Function with no DML statements can be directly
called in SELECT query whereas the function with
DML operation can only be called from other
PL/SQL blocks.
* Function can also return the value through OUT
parameters other than using RETURN.
LOGO
Sub titleFunctions in PL/SQL
What is Function
PL/SQL
CREATE OR REPLACE FUNCTION
<function_name>
(
< variable_name parameter IN/OUT <datatype>
)
RETURN <datatype>
[ IS | AS ]
<declaration_part>
BEGIN
<execution part>
RETURN (value/var);
END<function_name>; LOGO
Sub titleFunctions in PL/SQL
SYNTAX OF FUNCTION
PL/SQL
create or replace function f_mul( x in int,y in int)
return int
as
z int;
begin
z:= x*y;
return(z);
End f_mul;
/
Points to Ponder:-
* calling function from select query
select f_mul(12,10) from dual;
* calling a function from a program
Declare
a int;b int;c int;
Begin
a:= &a;b:=&b;
c:=f_mul(a,b);
dbms_output.put_line('Result:' || c);End;/
LOGO
Sub titleFunctions in PL/SQL
EXAMPLE ON FUNCTION
PL/SQL
Points to Ponder:-
* calling a function from procedure
(I) create or replace procedure proc_mul(x int,y int,z out int)
as
begin
z:=f_mul(x,y);
End;
/
(II) Declare
a int;
b int;
c int;
Begin
a:=&a;
b:=&b;
proc_mul(a,b,c);
dbms_output.put_line('value of a:' ||a || 'value of b:' ||b);
dbms_output.put_line(‘Multiplication of aXb is :-' || c);
End;
/
LOGO
Sub titleFunctions in PL/SQL
EXAMPLE ON FUNCTION
PL/SQL
A Procedure is a subprogram unit that consists of
a group of PL/SQL statements. Each procedure in
Oracle has its own unique name by which it can
be referred. This subprogram unit is stored as a
database object. Below are the characteristics of
this subprogram unit.
 Procedures are standalone blocks of a program
that can be stored in the database.
 Call to these procedures can be made by referring
to their name, to execute the PL/SQL statements.
LOGO
Sub titleProcedure in PL/SQL
DEFINITION OF PROCEDURE
PL/SQL
 It is mainly used to execute a process in PL/SQL.
 It can have nested blocks, or it can be defined
and nested inside the other blocks or packages.
 The values can be passed into the procedure or
fetched from the procedure through parameters.
 Procedure can have a RETURN statement to
return the control to the calling block, but it
cannot return any values through the RETURN
statement.
 Procedures cannot be called directly from SELECT
statements. They can be called from another
block or through EXEC keyword. LOGO
Sub titleProcedure in PL/SQL
DEFINITION OF PROCEDURE
PL/SQL
CREATE [OR REPLACE ] PROCEDURE <name>[(argvarDatatype,
argvarDatatype)]
IS/AS
<declaration>
BEGIN
Statement1
Statement2
-----------
-----------
EXCEPTION
Statement1
Statement2
----------
----------
END;/Note:-[] i.e OPTIONAL
LOGO
Sub titleProcedure in PL/SQL
SYNTAX OF PROCEDURE
PL/SQL
CREATE OR REPLACE PROCEDURE proc_empcntno(vdesg emp.job%type)
AS
empcntno int;
BEGIN
select count(*) into empcntno from emp
where job=vdesg;
dbms_output.put_line('Entered Designation is:'||vdesg);
dbms_output.put_line('NO. of Employees:'||empcntno);
END ;
/
LOGO
Sub titleProcedure in PL/SQL
EXAMPLE ON PROCEDURE
PL/SQL
* Executing Multiple Queries .
* Reduces no. of hits to DB.
* Improve DB performance and N/W
performance.
* Enhancebility.
* Reusability.
* Modularity.
LOGO
Sub titleProcedure in PL/SQL
IMPORTANT POINT ABOUT PROCEDURE
PL/SQL
1) Exception
2)Exception Handling
3) Structure of Exception Handling.
4) Types of Exception Handling.
LOGO
Sub titleException in PL/SQL
PL/SQL
An error occurs during the program
execution is called Exception in PL/SQL.
PL/SQL facilitates programmers to catch
such conditions using exception block in
the program and an appropriate action is
taken against the error condition.
LOGO
Sub titleException in PL/SQL
WHAT IS EXCEPTION
PL/SQL
PL/SQL provides a feature to handle the
Exceptions which occur in a PL/SQL Block known
as exception Handling. Using Exception Handling
we can test the code and avoid it from exiting
abruptly.
When an exception occurs a messages which
explains its cause is received.
PL/SQL Exception message consists of three parts.
1) Type of Exception(Exception Handler)
2) An Error Code
3) A message
LOGO
Sub titleException in PL/SQL
EXCEPTION HANDLING
PL/SQL
DECLARE
Declaration section
BEGIN
Exception section
EXCEPTION
WHEN ex_name1 THEN
-Error handling statements
WHEN ex_name2 THEN
-Error handling statements
WHEN Others THEN
-Error handling statements
END LOGO
Sub titleException in PL/SQL
STRUCTURE OF HANDLING EXCEPTION
PL/SQL
There are 3 types of Exceptions.
a) Named System Exceptions
b) Unnamed System Exceptions
c) User-defined Exceptions
LOGO
Sub titleException in PL/SQL
TYPES OF EXCEPTION
PL/SQL
System exceptions are automatically
raised by Oracle, when a program
violates a RDBMS rule. There are some
system exceptions which are raised
frequently, so they are pre-defined and
given a name in Oracle which are known
as Named System Exceptions.
LOGO
Sub titleException in PL/SQL
NAMED SYSTEM EXCEPTIONS
PL/SQL
For example: NO_DATA_FOUND and
ZERO_DIVIDE are called Named System
exceptions.
Named system exceptions are:
1) Not Declared explicitly,
2) Raised implicitly when a predefined
Oracle error occurs,
3) caught by referencing the standard
name within an exception-handling
routine. LOGO
Sub titleException in PL/SQL
NAMED SYSTEM EXCEPTIONS
PL/SQL
Exception Name Reason(Message) Error Number
CURSOR_ALREADY_
OPEN
When you open a
cursor that is
already open.
ORA-06511
LOGO
Sub titleException in PL/SQL
NAMED SYSTEM EXCEPTIONS
PL/SQL
Exception Name Reason(Message) Error Number
INVALID_CURSOR When you perform an
invalid operation on a
cursor like closing a
cursor, fetch data from
a cursor that is not
opened.
ORA-01001
NO_DATA_FOUND When a
SELECT...INTO
clause does not
return any row from
a table.
ORA-01403
LOGO
Sub titleException in PL/SQL
NAMED SYSTEM EXCEPTIONS
PL/SQL
Exception Name Reason(Message) Error Number
TOO_MANY_ROWS
When you SELECT or
fetch more than one
row into a record or
variable.
ORA-01422
ZERO_DIVIDE When you attempt
to divide a number
by zero.
ORA-01476.
LOGO
Sub titleException in PL/SQL
NAMED SYSTEM EXCEPTIONS
PL/SQL
Create or Replace Procedure proc_excep(vdesg emp.job%type)
is
vename varchar2(20);
Begin
select ename into vename from emp
where job =vdesg;
dbms_output.put_line('Name:-' || vename);
Exception
when NO_DATA_FOUND then
dbms_output.put_line('No Employee existed with given job');
when TOO_MANY_ROWS then
dbms_output.put_line('Multiple employees existed ,use explicit
cursor');
End proc_excep;
/
LOGO
Sub titleException in PL/SQL
PROGRAM ON NAMED SYSTEM EXCEPTION
PL/SQL
Apart from system exceptions we can explicitly
define exceptions based on business rules. These
are known as user-defined exceptions.
Steps to be followed to use user-defined exceptions:
• They should be explicitly declared in the
declaration section.
• They should be explicitly raised in the Execution
Section.
• They should be handled by referencing the user-
defined exception name in the exception section.
LOGO
Sub titleException in PL/SQL
USER DEFINED EXCEPTIONS
PL/SQL
Declare
var_name Exception
Begin
Statement1
Statement2
Raise <var_name>;
Exception
When <var_name> then
Statement1
Statement2
End;
/
LOGO
Sub titleException in PL/SQL
SYNTAX OF USER DEFINED EXCEPTION
PL/SQL
Create or Replace Procedure proc_comm (veid int)
is
vcomm int;
c_miss Exception;
Begin
select comm into vcomm from emp where empno=veid;
if vcomm IS NOT NULL then
dbms_output.put_line('Comm of' || veid || 'is' || vcomm);
else
RAISE c_miss;
end if;
Exception
when NO_DATA_FOUND then
dbms_output.put_line('EmpId Not Existed');
when TOO_MANY_ROWS then
dbms_output.put_line('Duplicate EmpId');
when c_miss then
dbms_output.put_line('Comm is Not Existed');
End proc_comm;
LOGO
Sub titleException in PL/SQL
EXAMPLE ON USER DEFINED EXCEPTION
PL/SQL
 It is also known as PRAGMA EXCEPTION_INIT.
 It is the way to associate user defined exception
with oracle predefined error.
 If the oracle predefined error is not having name
then we can assign name to that error using
PRAGMA EXCEPTION_INIT.
LOGO
Sub titleException in PL/SQL
UNNAMED EXCEPTIONS
PL/SQL
DECLARE
user_define_exception_name EXCEPTION;
PRAGMA EXCEPTION_INIT(user_define_exception_name,-
error_number);
BEGIN
statement(s);
IF condition THEN
RAISE user_define_exception_name;
END IF;
EXCEPTION
WHEN user_define_exception_name THEN
User defined statement (action) will be taken;
END;
/
Points to Ponder:-
* exception_name and error_number define on yourself, where exception_name is
character string up to 2048 bytes support and error_number is a negative integer
range from -20000 to -20999.
LOGO
Sub titleException in PL/SQL
SYNTAX OF UNNAMED EXCEPTION
PL/SQL
DECLARE
myex EXCEPTION;
PRAGMA EXCEPTION_INIT(myex,-20015);
n NUMBER := &n;
BEGIN
FOR i IN 1..n LOOP
dbms_output.put_line(i);
IF i=n THEN
RAISE myex;
END IF;
END LOOP;
EXCEPTION
WHEN myex THEN
dbms_output.put_line('loop finish');
END;
/
LOGO
Sub titleException in PL/SQL
PROGRAM ON UNNAMED EXCEPTION
PL/SQL
 A cursor is a temporary work area created in the
system memory when a SQL statement is
executed. A cursor contains information on a
select statement and the rows of data accessed
by it.
 This temporary work area is used to store the
data retrieved from the database, and manipulate
this data. A cursor can hold more than one row,
but can process only one row at a time. The set of
rows the cursor holds is called the active set.
LOGO
Sub titleCursor in PL/SQL
DEFINITION OF CURSOR
PL/SQL
There are two types of cursors in PL/SQL:
 Implicit cursors
 Explicit cursors
LOGO
Sub titleCursor in PL/SQL
TYPES OF CURSOR
PL/SQL
 Managed by Oracle Engine
 Allocation and deallocation is done by Oracle
Engine.
 Used by DML,DQL.
LOGO
Sub titleCursor in PL/SQL
IMPLICIT CURSOR
PL/SQL
 SQL%ISOPEN
 SQL%FOUND
 SQL%NOTFOUND
 SQL%ROWCOUNT
LOGO
Sub titleCursor in PL/SQL
PROPERTIES OF IMPLICIT CURSOR
PL/SQL
Create or Replace procedure proc_sal_update(vjob in emp.job%type,incr in int)
is
Begin
update empcp
set sal=sal+((sal*incr)/100)
where job=vjob;
if (sql%found) then
dbms_output.put_line('Updation is Successful');
dbms_output.put_line('Number of '||vjob||'s updated:'
||SQL%ROWCOUNT);
else
dbms_output.put_line('Updation Failed');
dbms_output.put_line('Number of '||vjob||'s updated:'
||SQL%ROWCOUNT);
end if;
End proc_sal_update;
/
LOGO
Sub titleCursor in PL/SQL
PROGRAM ON IMPLICIT CURSOR
PL/SQL
 Managed by User
 Allocation and deallocation is done by User.
 To display multiple records using
subprogram/procedure.
Its has four steps to define it;-
I)Declare Cursor
II)Open Cursor
III)Fetch data from cursor
IV)Close Cursor
LOGO
Sub titleCursor in PL/SQL
EXPLICIT CURSOR
PL/SQL
I)Declare Cursor
CURSOR <cursor_name> IS select
column_name...... from <table_name>
where condition;
II)Open Cursor
OPEN <cursor_name>;
III)fetch data from Cursor
FETCH <cursor_name> INTO <var>
IV)Close Cursor
CLOSE <cursor_name>;
LOGO
Sub titleCursor in PL/SQL
SYNTAX OF EXPLICIT CURSOR
PL/SQL
 <cursor_name>%ISOPEN
 <cursor_name>%FOUND
 <cursor_name>%NOTFOUND
 <cursor_name>%ROWCOUNT
LOGO
Sub titleCursor in PL/SQL
PROPERTIES OF EXPLICIT CURSOR
PL/SQL
DECLARE
CURSOR my_cursor IS SELECT sal + NVL(comm, 0) wages, ename
FROM emp;
my_rec my_cursor%ROWTYPE;
BEGIN
OPEN my_cursor;
LOOP
FETCH my_cursor INTO my_rec;
EXIT WHEN my_cursor%NOTFOUND;
IF my_rec.wages > 2000 THEN
dbms_output.put_line(my_rec.wages||' '||my_rec.ename);
END IF;
END LOOP;
CLOSE my_cursor;
END;
/
LOGO
Sub titleCursor in PL/SQL
PROGRAM ON EXPLICIT CURSOR
PL/SQL
Triggers are stored programs, which are
automatically executed or fired when some
events occur. Triggers are, in fact, written to be
executed in response to any of the following
events −
*A database manipulation (DML) statement
(DELETE, INSERT, or UPDATE)
*A database definition (DDL) statement (CREATE,
ALTER, or DROP).
*A database operation (SERVERERROR, LOGON,
LOGOFF, STARTUP, or SHUTDOWN).
LOGO
Sub titleTrigger in PL/SQL
DEFINITION
PL/SQL
CREATE OR REPLACE TRIGGER display_salary_changes1
BEFORE DELETE OR INSERT OR UPDATE ON empcp1
FOR EACH ROW
WHEN (old.deptno > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.sal - :OLD.sal;
dbms_output.put_line('Old salary: ' || :OLD.sal);
dbms_output.put_line('New salary: ' || :NEW.sal);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/
LOGO
Sub titleTrigger in PL/SQL
PROGRAM ON DML CURSOR
PL/SQL
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END
LOGO
Sub titleTrigger in PL/SQL
SYNTAX OF TRIGGER
LOGO
Thank you!

More Related Content

PPTX
Procedure n functions
PPTX
Function and types
PPTX
Introduction to PL/SQL
PPTX
Procedure and Functions in pl/sql
ODP
Open Gurukul Language PL/SQL
PPTX
Packages in PL/SQL
PPT
PL/SQL Introduction and Concepts
PPT
SQL / PL
Procedure n functions
Function and types
Introduction to PL/SQL
Procedure and Functions in pl/sql
Open Gurukul Language PL/SQL
Packages in PL/SQL
PL/SQL Introduction and Concepts
SQL / PL

What's hot (18)

PPT
DOC
4. function
PPT
PDF
Chapter 13.1.6
PDF
PLSQL tutorial...
PDF
Ooabap notes with_programs
PPTX
Functions in C
PPTX
4. plsql
PPT
10g plsql slide
PPTX
Oracle: Procedures
PPT
Lecture11 abap on line
PDF
379008-rc217-functionalprogramming
PPTX
pl/sql Procedure
PPTX
Function & Recursion
PPTX
3 Function & Storage Class.pptx
PPTX
User Defined Functions in MATLAB Part-4
PDF
Oracle db subprograms
PPT
POLITEKNIK MALAYSIA
4. function
Chapter 13.1.6
PLSQL tutorial...
Ooabap notes with_programs
Functions in C
4. plsql
10g plsql slide
Oracle: Procedures
Lecture11 abap on line
379008-rc217-functionalprogramming
pl/sql Procedure
Function & Recursion
3 Function & Storage Class.pptx
User Defined Functions in MATLAB Part-4
Oracle db subprograms
POLITEKNIK MALAYSIA
Ad

Similar to DAC training-batch -2020(PLSQL) (20)

PDF
Dbms 2011
PPTX
PLSQL.pptx
PPTX
Pl sql Prograaming of Database management system
PPTX
PLSQL Tutorial
PDF
Lecture Notes Unit5 chapter 15 PL/SQL Programming
PPT
L9 l10 server side programming
PPT
plsql.ppt
PPT
Ppt on plssql study aboutnsmsmskskwkwkwkwk
PPT
Mis4200notes8 2
PDF
rdbms.pdf plsql database system notes for students to study
PPTX
PLSQLmy Updated (1).pptx
PPTX
Pl/SQL Procedure,Function & Packages - sub programs
PDF
Lecture Notes Unit5 chapter17 Stored procedures and functions
PPT
SQL- Introduction to PL/SQL
PDF
plsql notes.pdf for students Oracle databases
PDF
PL-SQL.pdf
DOCX
Oracle pl sql
PDF
PL/SQL for Beginners - PL/SQL Tutorial 1
PPTX
Ch as pbdasdadssadsadsadasdasdasdas fdt .pptx
PPTX
Rdbms chapter 1 function
Dbms 2011
PLSQL.pptx
Pl sql Prograaming of Database management system
PLSQL Tutorial
Lecture Notes Unit5 chapter 15 PL/SQL Programming
L9 l10 server side programming
plsql.ppt
Ppt on plssql study aboutnsmsmskskwkwkwkwk
Mis4200notes8 2
rdbms.pdf plsql database system notes for students to study
PLSQLmy Updated (1).pptx
Pl/SQL Procedure,Function & Packages - sub programs
Lecture Notes Unit5 chapter17 Stored procedures and functions
SQL- Introduction to PL/SQL
plsql notes.pdf for students Oracle databases
PL-SQL.pdf
Oracle pl sql
PL/SQL for Beginners - PL/SQL Tutorial 1
Ch as pbdasdadssadsadsadasdasdasdas fdt .pptx
Rdbms chapter 1 function
Ad

Recently uploaded (20)

PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
composite construction of structures.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPT
Project quality management in manufacturing
PPTX
Welding lecture in detail for understanding
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Geodesy 1.pptx...............................................
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Automation-in-Manufacturing-Chapter-Introduction.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
composite construction of structures.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CH1 Production IntroductoryConcepts.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Project quality management in manufacturing
Welding lecture in detail for understanding
Foundation to blockchain - A guide to Blockchain Tech
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Geodesy 1.pptx...............................................
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...

DAC training-batch -2020(PLSQL)

  • 2. LOGO PL/SQL Introduction to PL/SQL Data types in PL/SQL Control Structures in PL/SQL Functions in PL/SQL
  • 4. PL/SQL  PL/SQL is a combination of SQL along with the procedural features of programming languages.  Properties of programming as well as the great interaction with database. Points to Ponder:- PLSQL IS NOT A CASE SENSITIVE LANGUAGE. LOGO Sub titleIntroduction to PL/SQL
  • 5. PL/SQL Points to be Ponder:- LOGO Sub titleComments in PL/SQL
  • 6. PL/SQL  DECLARE :- if you want to declare a variable in plsql program then it takes place in declare section.  BEGIN:- is used to start the working of program.  END:- is used to terminate the begin. Points to Ponder:- DELIMITER IS USED TO RUN (/) LOGO Sub titleImportant PL/SQL Concepts
  • 7. PL/SQL  SET SERVEROUTPUT ON :- is run after every time when you login in a session. LOGO Sub titleWhat to do Previously
  • 8. PL/SQL DBMS_OUTPUT.PUT_LINE command for e.g. if sal=10 and you want to print it. Then it looks like dbms_output.put_line(‘the salary is ‘ ||sal); LOGO Sub titleTo show OUTPUT on Your Screen
  • 10. PL/SQL DECLARE N NUMBER(11); BEGIN DBMS_OUTPUT.PUT_LINE('ENTER A NUMBER:-'); N:=&N; DBMS_OUTPUT.PUT_LINE('THE VALUE IS'||N); END; / LOGO Sub titleUser’s Input for a Variable
  • 12. PL/SQL Syntax:- IF condition THEN statement 1; ELSE statement 2; END IF; Points to Ponder:- IF STATEMENT WORKS AS SIMILAR AS C OR C++ LOGO Sub titleControl Structures in PL/SQL IF STATEMENT
  • 13. PL/SQL DECLARE Age number(11); Begin Age:=&age; If age>18 then Dbms_output.put_line(‘You can vote’); Else Dbms_output.put_line(‘You cannot vote’); End if; End; / LOGO Sub titleControl Structures in PL/SQL IF STATEMENT EXAMPLE
  • 14. PL/SQL Declare A number(11); Begin Select sal into a from empcp where ename='MARTIN'; If a>1000 then Update empcp set comm=comm+1000 where ename='MARTIN'; Else Update empcp set comm=comm+500 where ename='MARTIN';End if;End;/ LOGO Sub titleControl Structures in PL/SQL IF WITH SQL TABLE
  • 15. PL/SQL 1)SIMPLE LOOP 2)WHILE LOOP 3)FOR LOOP LOGO Sub titleControl Structures in PL/SQL LOOPS IN PL/SQL
  • 16. PL/SQL Print number from 1 to 10 using FOR loop BEGIN FOR i in [REVERSE] 1 ..10 Loop Dbms_output.put_line(i); END Loop; End; / Points to Ponder:- (For FOR LOOP has NO need to initialize variable i explicitly but it need in while ) LOGO Sub titleControl Structures in PL/SQL FOR LOOP
  • 17. PL/SQL Print number from 1 to 10 using WHILE loop Declare i number(3):=0; Begin While i<10 loop i:=i+1; Dbms_output.put_line(i); End loop; End; / LOGO Sub titleControl Structures in PL/SQL WHILE LOOP
  • 18. PL/SQL LOOP Statement 1; Statement 2; Exit condition End loop; LOGO Sub titleControl Structures in PL/SQL SYNTAX OF SIMPLE LOOP
  • 19. PL/SQL Declare I number(2):=0; Begin Loop dbms_output.put_line('Before Increment value of variable I: ' || I); I:=I+1; dbms_output.put_line('After Increment value of variable I: ' || I); EXIT when (I>0);END loop;END;/ LOGO Sub titleControl Structures in PL/SQL SIMPLE LOOP EXAMPLE
  • 20. PL/SQL CASE (expression) WHEN <value1> THEN action_blockl; WHEN <value2> THEN action_block2; WHEN <value3> THEN action_block3; ELSE action_block_default; END CASE; LOGO Sub titleControl Structures in PL/SQL SYNTAX OF “CASE”
  • 21. PL/SQL Declare a NUMBER :=55; b NUMBER :=5; arth_operation VARCHAR2(20) :='MULTIPLY'; BEGIN dbms_output.put_line('Program started.' ); CASE (arth_operation) WHEN 'ADD' THEN dbms_output.put_line('Addition of the numbers are:' || a+b ); WHEN 'SUBTRACT' THEN dbms_output.put_line('Subtraction of the numbers are: '||a-b ); WHEN 'MULTIPLY' THEN dbms_output.put_line('Multiplication of the numbers are: '|| a*b ); WHEN 'DIVIDE' THEN dbms_output.put_line('Division of the numbers are:'|| a/b); ELSE dbms_output.put_line('No operation action defined. Invalid operation'); END CASE; dbms_output.put_line('Program completed.' ); END; / LOGO Sub titleControl Structures in PL/SQL EXAMPLE OF “CASE”
  • 22. PL/SQL  This is similar to passing parameters in programming languages.  We can pass values to the stored procedure and function through these parameters or variables.  This type of parameter is a read only parameter. Points to Ponder:- We can assign the value of IN type parameter to a variable or use it in a query, but we cannot change its value inside the procedure. LOGO Sub titleFunctions in PL/SQL “IN” PARAMETER
  • 23. PL/SQL CREATE [OR REPLACE] FUNCTION function_name (param_name1 IN data_type, param_name2 IN data_type.. ) Here:- (i)param_name1, param_name2... are unique parameter names. (ii) data_type - defines the DataType of the variable. (iii) IN - is optional, by default it is a IN type parameter. LOGO Sub titleFunctions in PL/SQL “IN” PARAMETER SYNTAX
  • 24. PL/SQL  The OUT parameters are used to send the OUTPUT from a procedure or a function.  This is a write-only parameter i.e. we cannot pass values to OUT parameter while executing the stored procedure, but we can assign values to OUT parameter inside the stored procedure and the calling program can receive this output value. LOGO Sub titleFunctions in PL/SQL “OUT” PARAMETER
  • 25. PL/SQL CREATE [OR REPLACE] PROCEDURE procedure_name (param_name1 IN data_type, param_name2 IN data_type , param_name3 OUT data_type) Here:- (i) param_name1, param_name2 are unique parameter Names and can not be modified with in procedure. (ii) param_name3 can be modified with in procedure. (iii) data_type - defines the DataType of the variable. LOGO Sub titleFunctions in PL/SQL “OUT” PARAMETER SYNTAX
  • 26. PL/SQL CREATE OR REPLACE PROCEDURE procname_outparam (inparam1 in VARCHAR2,outParam2 OUT VARCHAR2) IS BEGIN outParam2 := 'Hello World OUT parameter I am changeable parameter' || inparam1; END; / Run it:- DECLARE outParam2 VARCHAR2(100); inparam1 VARCHAR2(100); BEGIN Procname_outparam ('i am IN parameter',outParam2); DBMS_OUTPUT.PUT_LINE(outParam2); END; / LOGO Sub titleFunctions in PL/SQL EXAMPLE ON “IN” AND “OUT”
  • 27. PL/SQL  The IN OUT parameter allows us to pass values into a procedure and get output values from the procedure.  This parameter is used if the value of the IN parameter can be changed in the calling program.  By using IN OUT parameter we can pass values into a parameter and return a value to the calling program using the same parameter. But this is possible only if the value passed to the procedure and output value have a same data type. LOGO Sub titleFunctions in PL/SQL “INOUT” PARAMETER
  • 28. PL/SQL CREATE [OR REPLACE] PROCEDURE procedure_name (param_name IN OUT data_type) Here:- (i) param_name are unique parameter Names and can not be modified with in procedure. (ii) param_name3 can be modified with in procedure. (iii) data_type - defines the DataType of the variable. LOGO Sub titleFunctions in PL/SQL “IN OUT” PARAMETER SYNTAX
  • 29. PL/SQL Functions is a standalone PL/SQL subprogram. Like PL/SQL procedure, functions have a unique name by which it can be referred. These are stored as PL/SQL database objects. Below are some of the characteristics of functions.  Functions are a standalone block that is mainly used for calculation purpose.  Function use RETURN keyword to return the value, and the data type of this is defined at the time of creation. LOGO Sub titleFunctions in PL/SQL DEFINITION OF FUNCTION
  • 30. PL/SQL  A Function should either return a value or raise the exception, i.e. return is mandatory in functions. Points to Ponder:- * Function with no DML statements can be directly called in SELECT query whereas the function with DML operation can only be called from other PL/SQL blocks. * Function can also return the value through OUT parameters other than using RETURN. LOGO Sub titleFunctions in PL/SQL What is Function
  • 31. PL/SQL CREATE OR REPLACE FUNCTION <function_name> ( < variable_name parameter IN/OUT <datatype> ) RETURN <datatype> [ IS | AS ] <declaration_part> BEGIN <execution part> RETURN (value/var); END<function_name>; LOGO Sub titleFunctions in PL/SQL SYNTAX OF FUNCTION
  • 32. PL/SQL create or replace function f_mul( x in int,y in int) return int as z int; begin z:= x*y; return(z); End f_mul; / Points to Ponder:- * calling function from select query select f_mul(12,10) from dual; * calling a function from a program Declare a int;b int;c int; Begin a:= &a;b:=&b; c:=f_mul(a,b); dbms_output.put_line('Result:' || c);End;/ LOGO Sub titleFunctions in PL/SQL EXAMPLE ON FUNCTION
  • 33. PL/SQL Points to Ponder:- * calling a function from procedure (I) create or replace procedure proc_mul(x int,y int,z out int) as begin z:=f_mul(x,y); End; / (II) Declare a int; b int; c int; Begin a:=&a; b:=&b; proc_mul(a,b,c); dbms_output.put_line('value of a:' ||a || 'value of b:' ||b); dbms_output.put_line(‘Multiplication of aXb is :-' || c); End; / LOGO Sub titleFunctions in PL/SQL EXAMPLE ON FUNCTION
  • 34. PL/SQL A Procedure is a subprogram unit that consists of a group of PL/SQL statements. Each procedure in Oracle has its own unique name by which it can be referred. This subprogram unit is stored as a database object. Below are the characteristics of this subprogram unit.  Procedures are standalone blocks of a program that can be stored in the database.  Call to these procedures can be made by referring to their name, to execute the PL/SQL statements. LOGO Sub titleProcedure in PL/SQL DEFINITION OF PROCEDURE
  • 35. PL/SQL  It is mainly used to execute a process in PL/SQL.  It can have nested blocks, or it can be defined and nested inside the other blocks or packages.  The values can be passed into the procedure or fetched from the procedure through parameters.  Procedure can have a RETURN statement to return the control to the calling block, but it cannot return any values through the RETURN statement.  Procedures cannot be called directly from SELECT statements. They can be called from another block or through EXEC keyword. LOGO Sub titleProcedure in PL/SQL DEFINITION OF PROCEDURE
  • 36. PL/SQL CREATE [OR REPLACE ] PROCEDURE <name>[(argvarDatatype, argvarDatatype)] IS/AS <declaration> BEGIN Statement1 Statement2 ----------- ----------- EXCEPTION Statement1 Statement2 ---------- ---------- END;/Note:-[] i.e OPTIONAL LOGO Sub titleProcedure in PL/SQL SYNTAX OF PROCEDURE
  • 37. PL/SQL CREATE OR REPLACE PROCEDURE proc_empcntno(vdesg emp.job%type) AS empcntno int; BEGIN select count(*) into empcntno from emp where job=vdesg; dbms_output.put_line('Entered Designation is:'||vdesg); dbms_output.put_line('NO. of Employees:'||empcntno); END ; / LOGO Sub titleProcedure in PL/SQL EXAMPLE ON PROCEDURE
  • 38. PL/SQL * Executing Multiple Queries . * Reduces no. of hits to DB. * Improve DB performance and N/W performance. * Enhancebility. * Reusability. * Modularity. LOGO Sub titleProcedure in PL/SQL IMPORTANT POINT ABOUT PROCEDURE
  • 39. PL/SQL 1) Exception 2)Exception Handling 3) Structure of Exception Handling. 4) Types of Exception Handling. LOGO Sub titleException in PL/SQL
  • 40. PL/SQL An error occurs during the program execution is called Exception in PL/SQL. PL/SQL facilitates programmers to catch such conditions using exception block in the program and an appropriate action is taken against the error condition. LOGO Sub titleException in PL/SQL WHAT IS EXCEPTION
  • 41. PL/SQL PL/SQL provides a feature to handle the Exceptions which occur in a PL/SQL Block known as exception Handling. Using Exception Handling we can test the code and avoid it from exiting abruptly. When an exception occurs a messages which explains its cause is received. PL/SQL Exception message consists of three parts. 1) Type of Exception(Exception Handler) 2) An Error Code 3) A message LOGO Sub titleException in PL/SQL EXCEPTION HANDLING
  • 42. PL/SQL DECLARE Declaration section BEGIN Exception section EXCEPTION WHEN ex_name1 THEN -Error handling statements WHEN ex_name2 THEN -Error handling statements WHEN Others THEN -Error handling statements END LOGO Sub titleException in PL/SQL STRUCTURE OF HANDLING EXCEPTION
  • 43. PL/SQL There are 3 types of Exceptions. a) Named System Exceptions b) Unnamed System Exceptions c) User-defined Exceptions LOGO Sub titleException in PL/SQL TYPES OF EXCEPTION
  • 44. PL/SQL System exceptions are automatically raised by Oracle, when a program violates a RDBMS rule. There are some system exceptions which are raised frequently, so they are pre-defined and given a name in Oracle which are known as Named System Exceptions. LOGO Sub titleException in PL/SQL NAMED SYSTEM EXCEPTIONS
  • 45. PL/SQL For example: NO_DATA_FOUND and ZERO_DIVIDE are called Named System exceptions. Named system exceptions are: 1) Not Declared explicitly, 2) Raised implicitly when a predefined Oracle error occurs, 3) caught by referencing the standard name within an exception-handling routine. LOGO Sub titleException in PL/SQL NAMED SYSTEM EXCEPTIONS
  • 46. PL/SQL Exception Name Reason(Message) Error Number CURSOR_ALREADY_ OPEN When you open a cursor that is already open. ORA-06511 LOGO Sub titleException in PL/SQL NAMED SYSTEM EXCEPTIONS
  • 47. PL/SQL Exception Name Reason(Message) Error Number INVALID_CURSOR When you perform an invalid operation on a cursor like closing a cursor, fetch data from a cursor that is not opened. ORA-01001 NO_DATA_FOUND When a SELECT...INTO clause does not return any row from a table. ORA-01403 LOGO Sub titleException in PL/SQL NAMED SYSTEM EXCEPTIONS
  • 48. PL/SQL Exception Name Reason(Message) Error Number TOO_MANY_ROWS When you SELECT or fetch more than one row into a record or variable. ORA-01422 ZERO_DIVIDE When you attempt to divide a number by zero. ORA-01476. LOGO Sub titleException in PL/SQL NAMED SYSTEM EXCEPTIONS
  • 49. PL/SQL Create or Replace Procedure proc_excep(vdesg emp.job%type) is vename varchar2(20); Begin select ename into vename from emp where job =vdesg; dbms_output.put_line('Name:-' || vename); Exception when NO_DATA_FOUND then dbms_output.put_line('No Employee existed with given job'); when TOO_MANY_ROWS then dbms_output.put_line('Multiple employees existed ,use explicit cursor'); End proc_excep; / LOGO Sub titleException in PL/SQL PROGRAM ON NAMED SYSTEM EXCEPTION
  • 50. PL/SQL Apart from system exceptions we can explicitly define exceptions based on business rules. These are known as user-defined exceptions. Steps to be followed to use user-defined exceptions: • They should be explicitly declared in the declaration section. • They should be explicitly raised in the Execution Section. • They should be handled by referencing the user- defined exception name in the exception section. LOGO Sub titleException in PL/SQL USER DEFINED EXCEPTIONS
  • 51. PL/SQL Declare var_name Exception Begin Statement1 Statement2 Raise <var_name>; Exception When <var_name> then Statement1 Statement2 End; / LOGO Sub titleException in PL/SQL SYNTAX OF USER DEFINED EXCEPTION
  • 52. PL/SQL Create or Replace Procedure proc_comm (veid int) is vcomm int; c_miss Exception; Begin select comm into vcomm from emp where empno=veid; if vcomm IS NOT NULL then dbms_output.put_line('Comm of' || veid || 'is' || vcomm); else RAISE c_miss; end if; Exception when NO_DATA_FOUND then dbms_output.put_line('EmpId Not Existed'); when TOO_MANY_ROWS then dbms_output.put_line('Duplicate EmpId'); when c_miss then dbms_output.put_line('Comm is Not Existed'); End proc_comm; LOGO Sub titleException in PL/SQL EXAMPLE ON USER DEFINED EXCEPTION
  • 53. PL/SQL  It is also known as PRAGMA EXCEPTION_INIT.  It is the way to associate user defined exception with oracle predefined error.  If the oracle predefined error is not having name then we can assign name to that error using PRAGMA EXCEPTION_INIT. LOGO Sub titleException in PL/SQL UNNAMED EXCEPTIONS
  • 54. PL/SQL DECLARE user_define_exception_name EXCEPTION; PRAGMA EXCEPTION_INIT(user_define_exception_name,- error_number); BEGIN statement(s); IF condition THEN RAISE user_define_exception_name; END IF; EXCEPTION WHEN user_define_exception_name THEN User defined statement (action) will be taken; END; / Points to Ponder:- * exception_name and error_number define on yourself, where exception_name is character string up to 2048 bytes support and error_number is a negative integer range from -20000 to -20999. LOGO Sub titleException in PL/SQL SYNTAX OF UNNAMED EXCEPTION
  • 55. PL/SQL DECLARE myex EXCEPTION; PRAGMA EXCEPTION_INIT(myex,-20015); n NUMBER := &n; BEGIN FOR i IN 1..n LOOP dbms_output.put_line(i); IF i=n THEN RAISE myex; END IF; END LOOP; EXCEPTION WHEN myex THEN dbms_output.put_line('loop finish'); END; / LOGO Sub titleException in PL/SQL PROGRAM ON UNNAMED EXCEPTION
  • 56. PL/SQL  A cursor is a temporary work area created in the system memory when a SQL statement is executed. A cursor contains information on a select statement and the rows of data accessed by it.  This temporary work area is used to store the data retrieved from the database, and manipulate this data. A cursor can hold more than one row, but can process only one row at a time. The set of rows the cursor holds is called the active set. LOGO Sub titleCursor in PL/SQL DEFINITION OF CURSOR
  • 57. PL/SQL There are two types of cursors in PL/SQL:  Implicit cursors  Explicit cursors LOGO Sub titleCursor in PL/SQL TYPES OF CURSOR
  • 58. PL/SQL  Managed by Oracle Engine  Allocation and deallocation is done by Oracle Engine.  Used by DML,DQL. LOGO Sub titleCursor in PL/SQL IMPLICIT CURSOR
  • 59. PL/SQL  SQL%ISOPEN  SQL%FOUND  SQL%NOTFOUND  SQL%ROWCOUNT LOGO Sub titleCursor in PL/SQL PROPERTIES OF IMPLICIT CURSOR
  • 60. PL/SQL Create or Replace procedure proc_sal_update(vjob in emp.job%type,incr in int) is Begin update empcp set sal=sal+((sal*incr)/100) where job=vjob; if (sql%found) then dbms_output.put_line('Updation is Successful'); dbms_output.put_line('Number of '||vjob||'s updated:' ||SQL%ROWCOUNT); else dbms_output.put_line('Updation Failed'); dbms_output.put_line('Number of '||vjob||'s updated:' ||SQL%ROWCOUNT); end if; End proc_sal_update; / LOGO Sub titleCursor in PL/SQL PROGRAM ON IMPLICIT CURSOR
  • 61. PL/SQL  Managed by User  Allocation and deallocation is done by User.  To display multiple records using subprogram/procedure. Its has four steps to define it;- I)Declare Cursor II)Open Cursor III)Fetch data from cursor IV)Close Cursor LOGO Sub titleCursor in PL/SQL EXPLICIT CURSOR
  • 62. PL/SQL I)Declare Cursor CURSOR <cursor_name> IS select column_name...... from <table_name> where condition; II)Open Cursor OPEN <cursor_name>; III)fetch data from Cursor FETCH <cursor_name> INTO <var> IV)Close Cursor CLOSE <cursor_name>; LOGO Sub titleCursor in PL/SQL SYNTAX OF EXPLICIT CURSOR
  • 63. PL/SQL  <cursor_name>%ISOPEN  <cursor_name>%FOUND  <cursor_name>%NOTFOUND  <cursor_name>%ROWCOUNT LOGO Sub titleCursor in PL/SQL PROPERTIES OF EXPLICIT CURSOR
  • 64. PL/SQL DECLARE CURSOR my_cursor IS SELECT sal + NVL(comm, 0) wages, ename FROM emp; my_rec my_cursor%ROWTYPE; BEGIN OPEN my_cursor; LOOP FETCH my_cursor INTO my_rec; EXIT WHEN my_cursor%NOTFOUND; IF my_rec.wages > 2000 THEN dbms_output.put_line(my_rec.wages||' '||my_rec.ename); END IF; END LOOP; CLOSE my_cursor; END; / LOGO Sub titleCursor in PL/SQL PROGRAM ON EXPLICIT CURSOR
  • 65. PL/SQL Triggers are stored programs, which are automatically executed or fired when some events occur. Triggers are, in fact, written to be executed in response to any of the following events − *A database manipulation (DML) statement (DELETE, INSERT, or UPDATE) *A database definition (DDL) statement (CREATE, ALTER, or DROP). *A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or SHUTDOWN). LOGO Sub titleTrigger in PL/SQL DEFINITION
  • 66. PL/SQL CREATE OR REPLACE TRIGGER display_salary_changes1 BEFORE DELETE OR INSERT OR UPDATE ON empcp1 FOR EACH ROW WHEN (old.deptno > 0) DECLARE sal_diff number; BEGIN sal_diff := :NEW.sal - :OLD.sal; dbms_output.put_line('Old salary: ' || :OLD.sal); dbms_output.put_line('New salary: ' || :NEW.sal); dbms_output.put_line('Salary difference: ' || sal_diff); END; / LOGO Sub titleTrigger in PL/SQL PROGRAM ON DML CURSOR
  • 67. PL/SQL CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE | AFTER | } {INSERT [OR] | UPDATE [OR] | DELETE} [OF col_name] ON table_name [REFERENCING OLD AS o NEW AS n] [FOR EACH ROW] WHEN (condition) DECLARE Declaration-statements BEGIN Executable-statements EXCEPTION Exception-handling-statements END LOGO Sub titleTrigger in PL/SQL SYNTAX OF TRIGGER