FALLSEM2024-25 PMDS506P LO VL2024250105844 2024-09-25 Reference-Material-I
FALLSEM2024-25 PMDS506P LO VL2024250105844 2024-09-25 Reference-Material-I
PL/SQL
Overview of PL/SQL
PL/SQL is the procedural extension to SQL with design features of programming languages.
Data manipulation and query statements of SQL are included within procedural units of code.
Pl/SQL Environment
PL/SQL engine
PL/SQL PL/SQL Procedural
P SQL
block block statement
executor
The PL/SQL engine in the oracle server process the pl/sql block and it separates SQL staments
and sends them individually to the SQL statements executor
Benefits of PL/SQL
• Integration
• Improved performance
• Modularized program development
• Portability
• Identifiers
Database Systems Lab Manual
DECLARE (optional)
BEGIN (Mandatory)
-SQL statements
-PL/SQL statements
EXCEPTION (optional)
END;
DECLARATIVE
It contains all variables, constants, cursors and user defined exceptions that are referenced
in the executable and declarative sections.
EXECUTABLE
It contains SQL statements to manipulate data in the database and PL/SQL statements to
manipulate data in the block.
EXCEPTION HANDLING
It specifies the actions to perform when errors and abnormal conditions arise in the
executable section.
• Anonymous Blocks
Database Systems Lab Manual
• Subprograms
Subprograms are named PL/SQL blocks that can accept parameters and can be invoked.
It can be declared either as procedures or as functions.
To write PL/SQL programs, create a script file and run the script file or use editor.
Step1:
Step2:
Step 3:
Step4:
SQL> @z:\oracle\sql\var1.sql
SQL> declare
2 a number:=3;
3 begin
Database Systems Lab Manual
4 dbms_output.put_line(a);
5 end;
6 /
1 declare
2 v_name varchar2(10);
3 v_regno number;
4 begin
5 v_name:='venkat';
6 v_regno:=39;
9 end;
SQL> /
the no is 39
101 x 102 1
102 y 103 2
103 z 102 3
104 p 102 4
105 q
begin
dbms_output.put_line(v_no);
end;
SQL>/
101
SCALAR VARIABLE
1 declare
2 v_name varchar2(10);
3 V_count binary_integer:=10;
4 V_totalsal number(9,2);
5 v_orderdate date:=sysdate;
9 begin
10 v_name:='venkat';
11 v_totalsal:=10000.23;
12 dbms_output.put_line(v_name);
13 dbms_output.put_line(v_count);
14 dbms_output.put_line(v_orderdate);
15 dbms_output.put_line(c_tax);
16 dbms_output.put_line(v_regno);
17 end;
18 /
venkat
10
19-AUG-05
6.23
23
Database Systems Lab Manual
1 declare
2 v_no emp.ssn%type;
3 V_name varchar2(10):='venkat';
4 name v_name%type;
5 begin
6 v_no:=10;
7 name:='ven';
8 dbms_output.put_line(v_no);
9 dbms_output.put_line(name);
10*end;
11 /
10
ven
BIND VARIABLES
A bind variable is a variable that is declared in a host environment. Bind variables can be
used to pass run-time values, which can be either number or character, into or out of one or more
PL/SQL programs.
Example:
SQL> ed
File:
1 begin
3 dbms_output.put_line(:a);
4 end;
SQL> /
101
SQL> print a;
----------
101
To reference host variables, prefix the references with a colon (:) to distinguish them
from declared PL/SQL variable.
SQL> declare
2 v_sal number(9,2):=&aa;
3 begin
4 :gg:=v_sal/12;
5 end;
6 /
GG
----------
83.3333333
• Delimiters
• Identifiers, which include reserved words
• Literals
• Character literals
• Numeric literals
Database Systems Lab Manual
COMMENTS:
/* beginning */ ending
• SQLCODE
• SQL ERRM
SQL> ed
1 declare
2 vdate date;
3 begin
5 dbms_output.put_line(vdate);
6 end;
SQL> /
19-AUG-05
All SQL functions are allowed except decode function and group functions.
SQL> declare
2 v_a number:=3;
3 begin
4 declare
5 v_b number:=4;
6 begin
7 dbms_output.put_line(v_b);
dbms_output.put_line(v_a);
8 end;
9 dbms_output.put_line(v_a);
10 end;
11 /
SQL> ed
1 <<outer>>
2 declare
3 v_a number:=3;
4 begin
5 declare
6 v_a number:=4;
7 begin
8 dbms_output.put_line(v_a);
9 dbms_output.put_line(outer.v_a);
10 end;
11 end;
12 /
PROGRAMMING GUIDELINES:
NOTES:
101 x 102 1
Database Systems Lab Manual
102 y 103 2
103 z 102 3
104 p 102 4
105 q
SQL> ed
1 declare
2 v_ssn number;
3 v_name varchar2(10);
4 begin
6 dbms_output.put_line(v_ssn);
7 dbms_output.put_line(v_name);
8 end;
SQL> /
101
a 3000 4000
Database Systems Lab Manual
b 5000 6000
c 3000 6000
d 4000 10000
e 2000 6000
SQL> ed
1 declare
2 v_lsal job_grade.lowest_sal%type;
3 v_hsal job_grade.highest_sal%type;
4 begin
6 dbms_output.put_line(v_lsal);
7 dbms_output.put_line(v_hsal);
8 end;
9 /
17000
32000
NAMING CONVENTIONS
A local variable in pl/sql name must not be equal to column names present in database .
declare
Database Systems Lab Manual
lastname varchar2(10);
begin
The above code will delete all employees because of the naming convention problem.
SUBSTIUTION VARIABLE:
SQL> ed
1 declare
2 v_sal number;
3 begin
4 v_sal:=&v_sal;
5 dbms_output.put_line(v_sal);
6 end;
7 /
2000
INSERTION
SQL> ed
Database Systems Lab Manual
1 begin
3 dbms_output.put_line('record inserted');
4 end;
5 /
record inserted
SQL> ed
1 begin
3 dbms_output.put_line('record inserted');
4 end;
SQL> /
record inserted
UPDATE:
SQL> ed
1 declare
2 v_sal number;
3 begin
4 v_sal:=&v_sal;
6 dbms_output.put_line('record updated');
7 end;
SQL> /
record updated
a 12000 4000
b 5000 6000
c 3000 6000
Database Systems Lab Manual
d 4000 10000
e 2000 6000
DELETE:
SQL> ed
1 declare
2 v_sal number;
3 begin
4 v_sal:=&v_sal;
6 dbms_output.put_line('record deleted');
7 end;
8 /
record deleted
a 12000 4000
c 3000 6000
d 4000 10000
Database Systems Lab Manual
e 2000 6000
CONTROL STRUCTURES
• IF statements
If –then-end if
If-then-else-end if
If-then-elseif-end if
• Case expressions
• Loop statements
Basic loops
While loops
For loops
Syntax of IF:
If condition then
Statements;
Statements;
Else
Statements;
End if;
Examples:
Database Systems Lab Manual
1 declare
2 a number;
3 b number;
4 begin
5 a:=&a;
6 b:=&b;
7 if a>b then
9 else
11 end if;
12 end;
SQL> /
new 6: b:=4;
greatest number is12
20 it 300 1800
1 declare
2 v_id departments.dept_id%type;
3 v_dname departments.dept_name%type;
4 begin
6 if v_id=11 then
7 dbms_output.put_line(v_dname);
9 dbms_output.put_line(v_id);
10 else
12 end if;
13 end;
SQL> / 10
Database Systems Lab Manual
1 declare
2 a number;
3 b number;
4 c number;
5 begin
6 a:=&a;
7 b:=&b;
8 c:=&c;
13 else
14 dbms_output.put_line('greatest number is'||c);
15 end if;
16 end;
SQL>/
C is greater :12.
Database Systems Lab Manual
4) Case Expressions
A case expression selects a result and returns it. To select the result, the case expression
uses an expression whose value is used to select one of several alternatives.
Syntax :
CASE selector
WHEN
expression1
THEN result1 WHEN
expression2
THEN result2
---------
Example:
1 declare
2 va varchar2(10);
3 v_result varchar2(10);
4 begin
5 va:=&va;
Database Systems Lab Manual
6 v_result:=
7 CASE va
11 ELSE 'poor'
12 end;
13 dbms_output.put_line('grade is'||v_result);
14 end;
15 /
new 5: va:='a';
grade is excellent
SQL> /
new 5: va:='b';
grade is very good
SQL> /
Database Systems Lab Manual
new 5: va:='c';
grade is good
5) For Structure
SQL> 1 begin
4 end loop;
5 commit;
6 end;
7/
CURSORS
The oracle server uses work areas, called private SQL areas, to execute SQL statement and to
store processing information. This area is called cursor.
Cursor types:
Explicit cursor
Fetch
Syntax:
Cursor declaration
Fetch
Explicit Cursor Attributes: To determine the status of the cursor, the cursor’s attributes are
checked.Cursors have the following four attributes that can be used in a PL/SQL program.
%isopen -To check if the cursor is opened or not
%found-To check if a record is found and can be fetched from the cursor
%rowcount-To check for the number of rows fetched from the cursor
%isopen, %found,%notfound are boolean attributes which are set to either TRUE or
FALSE.
A Simple Example:
1 declare
2 v_name wer1.name%type;
3 v_ssn wer1.ssn%type;
5 begin
6 open emp_c;
9 dbms_output.put_line(v_name);
Database Systems Lab Manual
10 end loop;
11 close emp_c;
12 end;
SQL> / x
x
x
y
y
2) %row count
1 declare
2 v_name wer1.name%type;
3 v_ssn wer1.ssn%type;
5 begin
6 open emp_c;
10 dbms_output.put_line(v_name);
Database Systems Lab Manual
11end loop;
12 close emp_c;
13 end;
SQL> / x
It processes the rows of the active set by fetching values into a PL/SQL record.
1 declare
3 emp_record emp_c%rowtype;
4 begin
5 open emp_c;
10 end loop;
11 commit;
12 close emp_c;
Database Systems Lab Manual
13 end;
14 /
NAME SSN
---------- ----------
x 101
x 101
x 101
x 101
x 101
x 101
x 101
x 101
x 101
x 101
y 102
y 102
12 rows selected.
It passes the parameter values to the cursor in a cursor FOR loop. This means that you can
open and close an explicit cursor several times in a block, returning a different active set on
each occasion.
Example:
1 declare
2 v_number number;
3 v_name varchar2(10);
6 begin
7 open c1(101,'x');
9 dbms_output.put_line(v_number);
10 close c1;
11 open c1(102,'y');
13 dbms_output.put_line(v_number);
14 close c1;
15 end;
16 /
101
102
5) Update
The update clause in the cursor query locks the affected rows when the cursor is opened.
Example:
declare
v_number number;
v_name varchar2(10);
cursor c1(eno number,ename varchar2) is select ssn,name from emp where ssn=eno and
begin
open c1(101,'x');
dbms_output.put_line(v_number);
close c1;
open c1(102,'y');
fetch c1 into v_number,v_name;
dbms_output.put_line(v_number);
close c1;
end;
EXCEPTIONS
Database Systems Lab Manual
Syntax
When exception1
then Statement1
Statement2
……..
When exception2
then Statement1
Statement2
……..
When others
then Statement1
Statement2 ……..
NO_DATA_FOUND
TOO_MANY_ROWS
INVALID_CURSOR
ZERO_DIVIDE
DUP_VAL_ON_INDEX
Example:
1)
1 declare
Database Systems Lab Manual
2 a number;
3 b number;
4 c number;
5 begin
6 a:=5;
7 b:=0;
8 c:= a/b;
9 exception
12 end;
SQL> /
2) Non-predefined error
1. Declare the name for the exception within the declarative section
2. Associate the declared exception with the standard oracle server error number using the
PRAGMA EXCEPTION_INIT statement
Syntax : PRAGMA EXCEPTION_INIT(exception, error_number);
3. Reference the declared exception within the corresponding exception –handling routine.
Database Systems Lab Manual
Example:
1 declare
2 emp_remain exception;
3 pragma exception_init
4 (emp_remain,-2292);
5 begin
7 commit;
8 exception
11end;
SQL> /
SQL> /
When an exception occurs, you can identify the associated error code or error message by
using two functions.
SQLERRM: It returns character data containing the message associated with the error
number.
Syntax:
declare;
v_error_code number;
v_error_messgage varchar2(255);
begin
when others then rollback;
v_error_code:=sqlcode;
v_error_message:=sqlerrm;
dbms_output.put_line(v_error_code||v_error_message);
end;
Example:
1 declare
2 invalid_dept exception;
3 begin
5 if sql%notfound then
6 raise invalid_dept;
7 end if;
8 exception
11 end;
• Anonymous Blocks
It is unnamed blocks. It is declared at the point in an application where they are to be
executed and are passed to the PL/SQL engine for execution at run time.
• Subprograms
Subprograms are named PL/SQL blocks that can accept parameters and can be invoked.
It can be declared either as procedures or as functions.
Overview of subprograms
A subprogram is named PL/SQL block that ca accept parameters and be invoked from a calling
environment.
Benefits of subprograms
• Easy maintenance
• Improved data security and integrity
• Improved performance
• Improved code clarity
Procedure
A procedure is a type of subprogram that performs an action. A procedure can be stored in the
database, as a schema object, for repeated execution.
Database Systems Lab Manual
Is| As
PL/SQL Block;
The replace option indicates that if the procedure exists, It will be dropped and replaced with the
new version created by the statement. Parameter name of a PL/SQL variable whose value is
passed to or populated by the calling environment.
IN parameter
IN parameters are passed as constants from the calling environment into the procedure.
Example:1
2 (grade in job_grade.gra%type)
Database Systems Lab Manual
3 is
4 begin
6* end raise_salary;
7 /
Procedure created.
a 13200 4000
c 3000 6000
d 4000 10000
e 2000 6000
Example:1
Database Systems Lab Manual
2 (g in job_grade.gra%type,
5 is
6 begin
8* end info;
9 /
SQL> @g:\oracle\sql\info1
Procedure created.
1.Run the sql script file to generate and compile the source code.
3.Invoke the procedure, supplying these host variables as the OUT parameters.: reference the
host variables in the execute command.
4.To view the values passed from the procedure to the calling environment ,use the print
command.
G_SAL
----------
13200
G1_SAL
----------
4000
IN OUT parameter
Example:1
2 (g in out number)
3 is
4 begin
6* end info;
7 /
Procedure created.
1 begin
2 :g_sal:=4000;
Database Systems Lab Manual
3* end;
G_SAL
----------
4000
G_SAL
----------
13200
Named : List actual parameters in library order by associating each with its
Combination : List some of the actual parameters as positional and some as named.
Removing procedures
Syntax:
Example:
Functions
A function is a named PL/SQL block that returns a value. A function can be stored in the
database as a schema object for repeated execution. A function is called as part of an expression.
Syntax:
….)]
Return datatype
Is/as
PL/SQL block ;
Example:
declare
summation number;
Database Systems Lab Manual
average number;
begin
return(m4+m5);
end;
begin
return(summ1/2);
end;
begin
summation:=summa(&m1,&m2);
average:=aver(summation);
dbms_output.put_line('summation is:'||summation);
dbms_output.put_line('average is:'||average);
end;
Removing functions
Example:
Packages
Database Systems Lab Manual
Packages bundle are related PL/SQL types, items, and subprograms into one container.
A package usually has a specification and a body, stored separately in the database.
Package specification
It is the interface to the application. It declares the types, variables, constants, exceptions,
cursors and subprograms.
A package specification can exist without a package body, but a package body cannot exist
without a package specification.
Syntax
is| as
Subprograms specifications
End package_name;
Example:
2 g_comm number:=0.10;
3 procedure reset_comm
4 (p_comm in number);
5 end commp;
Package created.
Database Systems Lab Manual
Package body
Syntax
Is| as
Subprogram bodies
End package_name;
Example
2 is
4 return boolean
5 is
6 v_max_comm number;
7 begin
10 else return(true);
11 end if;
12 end validate_comm;
Database Systems Lab Manual
14 is
15 begin
16 if validate_comm(p_comm)
17 then g_comm:=p_comm;
18 else
19 raise_application_error(-20210,'invalid commision');
20 end if;
21 end reset_comm;
22 end commp;
23 /
2 a constant number:=2;
4 end global_con;
5 /
Package created.
20 miles=40km
2 is
3 begin
4 y :=x *global_con.a;
5 end me;
SQL> /
Procedure created.
YA
----------
Removing packages:
Database Systems Lab Manual
Overloading
It is the use of same name for different subprograms inside a PL/SQL block, a subprogram, or a
package.
Example:
2 is
5 end over;
6 /
Package created
3 is
4 begin
6 end add_dept;
8 is
9 begin
11 end add_dept;
12 end overp;
Trigger
A trigger is aPL/SQL block or a PL/SQL procedure associated with a table ,view, schema, or the
database. It executes implicitly whenever a particular event takes place.
It can be:
Database trigger: fires whenever a data event or system event occurs on a schema or database.
• Triggering timing
o For table: BEFORE, AFTER
o For view: INSTEAD OF
• Triggering event: INSERT, UPDATE, or DELETE
• Table name: on table, view
• Trigger type: row or statement
• When clause: restricting condition
• Trigger body: PL/SQL block
Trigger type
Statement trigger:The trigger body executes once for the triggering event. this is default. A
statement trigger fires once, even if no rows are affected at all.
Row trigger:The trigger body executes once for each row affected by the triggering event. A
Database Systems Lab Manual
Syntax:
Timing
ON table_name
Trigger _body
Example:
create trigger ab
begin
raise_application_error(-20000,'not accessible')
end
This program raises an error during insertion and deletion and update operation in a row.
_____________________________________________________________________