PL SQL - Training - Pps
PL SQL - Training - Pps
Oracle
OraclePractice
Practice––PLSQL
PLSQLTraining
Training
11/17/20 1
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Programming in Oracle
with PL/SQL
11/17/20 2
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Introduction PL/SQL
11/17/20 3
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
PL/SQL Blocks
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Anonymous Block Structure:
DECLARE (optional)
/* Here you declare the variables you will use in this
block */
BEGIN (mandatory)
/* Here you define the executable statements (what the
block DOES!)*/
EXCEPTION (optional)
/* Here you define the actions that take place if an
exception is thrown during the run of this block */
END; (mandatory)
A correct completion of a block
/
will generate the following
Always put a new line with only :message
a / at the end of a block! (This
11/17/20 PL/SQL procedure successfully 5
tells Oracle to run the block)
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. |
completed
| PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
DECLARE
Syntax
identifier
identifier [CONSTANT]
[CONSTANT] datatype
datatype [NOT
[NOT NULL]
NULL]
[:=
[:= || DEFAULT
DEFAULT expr];
expr];
Notice that PL/SQL
Examples includes all SQL types,
…and more
Declare
Declare
birthday
birthday DATE;
DATE;
age
age NUMBER(2)
NUMBER(2) NOT
NOT NULL
NULL :=
:= 27;
27;
name
name VARCHAR2(13)
VARCHAR2(13) :=:= 'Levi';
'Levi';
magic
magic CONSTANT
CONSTANT NUMBER
NUMBER :=:= 77;
77;
valid
valid BOOLEAN
BOOLEAN NOT
NOT NULL
NULL :=
:= TRUE;
TRUE;
11/17/20 6
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Declaring Variables with the %TYPE Attribute
DECLARE
sname Sailors.sname%TYPE;
fav_boat VARCHAR2(30);
my_fav_boat fav_boat%TYPE := 'Pinta';
...
Accessing
another variable
11/17/20 7
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Declaring Variables with the %ROWTYPE Attribute
reserves_record Reserves%ROWTYPE;
reserves_record.sid:=9;
Reserves_record.bid:=877;
11/17/20 8
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Creating a PL/SQL Record
A record is a type of variable which we can define (like
‘struct’ in C or ‘object’ in Java)
DECLARE
DECLARE
TYPE
TYPE sailor_record_type
sailor_record_type IS IS RECORD
RECORD
(sname
(sname VARCHAR2(10),
VARCHAR2(10),
sid
sid VARCHAR2(9),
VARCHAR2(9),
age
age NUMBER(3),
NUMBER(3),
rating
rating NUMBER(3));
NUMBER(3));
sailor_record
sailor_record
sailor_record_type;
sailor_record_type;
...
...
BEGIN
BEGIN
Sailor
Sailor__record.sname:=‘peter’;
record.sname:=‘peter’;
11/17/20 9
Sailor
Sailor
www.intelligroup.com
__record.age:=45;
record.age:=45;
| Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Creating a Cursor
• We create a Cursor when we want to go over a result of
a query (like ResultSet in JDBC)
• Syntax Example:
DECLARE sailorData is a
variable that
cursor c is select * from sailors; can hold a
sailorData sailors%ROWTYPE; ROW from
the sailors
table
BEGIN
open c; Here the
fetch c into sailorData; first row of
sailors is
inserted into
sailorData
11/17/20 10
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Explicit Cursor Attributes
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Conditional logic
:Condition :Nested conditions
If <cond> If <cond>
then <command> then
elsif <cond2> if <cond2>
then <command2> then
else <command1>
<command3> end if;
end if; else <command2>
11/17/20
end if; 13
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
IF-THEN-ELSIF Statements
. . .
IF rating > 7 THEN
v_message := 'You are great';
ELSIF rating >= 5 THEN
v_message := 'Not bad';
ELSE
v_message := 'Pretty bad';
END IF;
. . .
11/17/20 14
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Suppose we have the following table:
);
Peter 3
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Solution
DECLARE
cnt NUMBER;
BEGIN
select count(*)
into cnt
from mylog
where who = user;
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
SQL Cursor
SQL cursor is automatically created after each SQL query.
It has 4 useful attributes:
SQL%ROWCOUNT Number of rows affected by the
most recent SQL statement (an
integer value).
SQL%FOUND Boolean attribute that evaluates to
TRUE if the most recent SQL
statement affects one or more rows.
SQL%NOTFOUND Boolean attribute that evaluates to
TRUE if the most recent SQL
statement does not affect any rows.
SQL%ISOPEN Always evaluates to FALSE because
PL/SQL closes implicit cursors
immediately after they are executed.
11/17/20 17
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Solution (2)
BEGIN
update mylog
set logon_num = logon_num + 1
where who = user;
if SQL%ROWCOUNT = 0 then
insert into mylog values(user, 1);
end if;
commit;
END;
/
11/17/20 18
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Loops: Simple Loop
create table number_table(
num NUMBER(10)
);
DECLARE
i number_table.num%TYPE := 1;
BEGIN
LOOP
INSERT INTO number_table
VALUES(i);
i := i + 1;
EXIT WHEN i > 10;
END LOOP;
END;
11/17/20 19
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Loops: Simple Cursor Loop
DECLARE
cursor c is select * from number_table;
cVal c%ROWTYPE;
BEGIN
open c;
LOOP
fetch c into cVal;
EXIT WHEN c%NOTFOUND;
insert into doubles values(cVal.num*2);
END LOOP;
11/17/20END; 20
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Loops: FOR Loop
DECLARE
i number_table.num%TYPE;
BEGIN
FOR i IN 1..10 LOOP
INSERT INTO number_table VALUES(i);
END LOOP;
END;
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Loops: For Cursor Loops
DECLARE
cursor c is select * from number_table;
BEGIN
for num_row in c loop
insert into doubles_table
values(num_row.num*2);
end loop;
END;
/
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Loops: WHILE Loop
DECLARE
TEN number:=10;
i number_table.num%TYPE:=1;
BEGIN
WHILE i <= TEN LOOP
INSERT INTO number_table
VALUES(i);
i := i + 1;
END LOOP;
END;
11/17/20 23
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Printing Output
11/17/20 24
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Input and output example
set serveroutput on format wrap size 1000000
ACCEPT high PROMPT 'Enter a number: '
DECLARE
i number_table.num%TYPE:=1;
BEGIN
dbms_output.put_line(‘Hey, I can print from PL/SQL!!!');
WHILE i <= &high LOOP
INSERT INTO number_table
VALUES(i);
i := i + 1;
END LOOP;
END;
11/17/20 25
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Reminder- structure of a block
DECLARE (optional)
/* Here you declare the variables you will use in this
block */
BEGIN (mandatory)
/* Here you define the executable statements (what the
block DOES!)*/
EXCEPTION (optional)
/* Here you define the actions that take place if an
exception is thrown during the run of this block */
END; (mandatory)
/
11/17/20 26
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Trapping Exceptions
11/17/20 27
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Trapping Exceptions (contd.)
DECLARE
num_row number_table%ROWTYPE;
BEGIN
select *
into num_row
from number_table;
dbms_output.put_line(1/num_row.num);
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line('No data!');
WHEN TOO_MANY_ROWS THEN
dbms_output.put_line('Too many!');
WHEN OTHERS THEN
11/17/20
dbms_output.put_line(‘Error’); 28
end;
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
User-Defined Exception
DECLARE
e_number1 EXCEPTION;
cnt NUMBER;
BEGIN
select count(*)
into cnt
from number_table;
EXCEPTION
WHEN e_number1 THEN
dbms_output.put_line('Count = 1');
end;
11/17/20 29
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Functions and Procedures
11/17/20 30
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Creating Procedures
CREATE
CREATE [OR
[OR REPLACE]
REPLACE] PROCEDURE
PROCEDURE procedure_name
procedure_name
[(parameter1
[(parameter1 [mode1]
[mode1] datatype1,
datatype1,
parameter2
parameter2 [mode2]
[mode2] datatype2,
datatype2,
.. .. .)]
.)]
IS|AS
IS|AS
PL/SQL
PL/SQL Block;
Block;
• Modes:
– IN: procedure must be called with a value for the parameter.
Value cannot be changed
– OUT: procedure must be called with a variable for the
parameter. Changes to the parameter are seen by the user (i.e.,
call by reference)
– IN OUT: value can be sent, and changes to the parameter are
seen by the user
•11/17/20
Default Mode is: IN 31
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Calling the Procedure
declare
declare
howmany
howmany mylog.logon_num%TYPE;
mylog.logon_num%TYPE;
begin
begin
num_logged(‘John',howmany);
num_logged(‘John',howmany);
dbms_output.put_line(howmany);
dbms_output.put_line(howmany);
end;
end;
//
11/17/20 32
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Errors in a Procedure
11/17/20 33
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Creating a Function
CREATE
CREATE [OR
[OR REPLACE]
REPLACE] FUNCTION
FUNCTION
function_name
function_name
[(parameter1
[(parameter1 [mode1]
[mode1] datatype1,
datatype1,
parameter2
parameter2 [mode2]
[mode2] datatype2,
datatype2,
.. .. .)]
.)]
RETURN
RETURN datatype
datatype
IS|AS
IS|AS
PL/SQL
PL/SQL Block;
Block;
11/17/20 34
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
A Function
create
create or or replace
replace function
function
rating_message(rating
rating_message(rating IN IN NUMBER)
NUMBER)
return
return VARCHAR2
VARCHAR2 NOTE THAT YOU
AS
AS DON'T SPECIFY THE
BEGIN
BEGIN SIZE
IF
IF rating
rating >> 77 THEN
THEN
return
return 'You
'You are
are great';
great';
ELSIF
ELSIF rating
rating >=>= 55 THEN
THEN
return
return 'Not
'Not bad';
bad';
ELSE
ELSE
return
return 'Pretty
'Pretty bad';
bad';
END
END IF;
IF;
END;
END;
//
11/17/20 35
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Calling the function
declare
declare
paulRate:=9;
paulRate:=9;
Begin
Begin
dbms_output.put_line(ratingMessage(paulRate
dbms_output.put_line(ratingMessage(paulRate
));
));
end;
end;
//
11/17/20 36
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
:Creating a function
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Packages
11/17/20 38
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
:Objectives
11/17/20 39
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Overview of Packages
Packages:
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
.Packages contd
11/17/20 41
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
.Packages contd
11/17/20 42
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Package development
Package specification *
package body *
11/17/20 43
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
How to develop a package
2.write the text of the CREATE PACKAGE BODY statement within a SQL
script file to create the package body and run the script file.
The source code is compiled into P code and is also stored within the
data dictionary
3. Invoke any public construct within the package from an Oracle server
environment.
11/17/20 44
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Creating the Package Specification
:Syntax
Create [or replace] package package_name
IS/AS
Public type and item declarations
Subprogram specifications
End package_name ;
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Creating the Package Specification
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
.Creating a package body contd
Comm._pack.sql
Procedure reset_comm(p_comm IN NUMBER)
IS
BEGIN
IF validate_comm(p_comm)
Then g_comm := p_comm; -- reset global variable
ELSE
;RAISE_ APPLICATION_ ERROR (-20210, ‘Invalid commission’)
;END IF
;END reset_comm
;End comm._package
/
11/17/20 47
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Invoking package constructs
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Invoking package constructs
11/17/20 49
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Invoking package constructs
11/17/20 50
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Declaring a Bodiless package
11/17/20 51
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Reference a public variable from a Stand-Alone Procedures
IS
BEGIN
;P_yard := p_meter *global_consts.meter_2_yard
;END meter_to_yard
/
VARIABLE yard NUMBER
Execute meter_to_yard(1, :yard)
PRINT yard
11/17/20 52
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Removing Packages
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Guidelines for developing packages
Place items in the declaration part of the package body when you
must maintain them throughout a session or across transactions
11/17/20 54
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
TRIGGERS
11/17/20 55
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Objective
• Create triggers
• Types of triggers
• Security issues
• Disable and enable triggers
11/17/20 56
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Triggers
11/17/20 57
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Trigger Concepts
11/17/20 58
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Trigger (contd.)
11/17/20 59
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Trigger Executions
11/17/20 60
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Trigger Syntax
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Row Trigger example(old)
11/17/20 62
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
: old Reference Variables
• : old column_name
- contains value before update
- null for insert trigger
- only valid in row trigger
• Cannot be modified
- use :new to change column values
• Commonly used in update or delete triggers
- e.g maintain audit trail of old values.
11/17/20 63
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
: New Reference Variables
• :NEW.column_name
- contains the value to be inserted or updated
• :NEW can be modified in a before triggers
• :NEW can be referenced in after trigger
• Commonly used in Insert and update
triggers
• :NEW is null in delete triggers.
• Only valid in row triggers.
11/17/20 64
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
: Row trigger example using :New
begin
/* convert character values to upper case */
:new.lastname := upper(:new.lastname);
:new.firstname := upper(:new.firstname);
end;
11/17/20 65
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Trigger attributes
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Trigger attributes (contd.)
11/17/20 67
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Audit trigger
11/17/20 68
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Statement trigger
11/17/20 69
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Instead of triggers
11/17/20 70
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Instead of triggers
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Autonomous Trigger
11/17/20 72
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Database Trigger Execution
System Events Oracle Server
Startup
After only
Servererror
User Events
DDL
Before
Create
or
Drop
After
11/17/20
Alter 73
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Rules to Remember
• When defining logon, startup and servererror triggers, you can specify the
After context.
• When defining logoff, shutdown triggers, you can specify the Before
context.
• You cannot define After startup and before shutdown for a schema. These
apply only to database.
11/17/20 74
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
DDL trigger
11/17/20 75
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
DDL trigger
• ora_dict_obj_name
• ora_dict_owner
• ora_dict_type
• ora_login_user
• ora_server_error(n)
• ora_sysevent
11/17/20 76
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Enable/Disable triggers
• Disable triggers
- Trigger still exists but is dormant.
- sql*loader and import will run faster
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Trigger and the Data Dictionary
Example :
Select object_name from user_objects where
object_type = ‘trigger’;
11/17/20 78
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Trigger Privileges
11/17/20 79
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Trigger Restrictions
11/17/20 80
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
SQL*Loader
11/17/20 81
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
SQL*Loader Features
•Load data from multiple datafiles during the same load
session.
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
SQL*Loader Multiple Data Files
• load data
• INFILE dept.dat
• INFILE newdept.dat
• append INTO TABLE TEST_DEPT
• (dept POSITION(1:2) integer external,
• deptname POSITION(4:20) CHAR)
11/17/20 83
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
SQL*Loader Multiple Tables
• INFILE employ_dept.dat
• APPEND INTO TABLE TEST_EMP
• WHEN recid = "1"
• (recid POSITION(1:1) integer external,
• empid POSITION(2:4) integer external,
• empname POSITION(5:12) CHAR,
• sal POSITION(13:16) integer external,
• comn POSITION(17:18) integer external,
• dept POSITION(19:20) integer external)
• INTO TABLE TEST_DEPT
• WHEN recid = "2"
• (recid POSITION(1:1) integer external,
• dept POSITION(2:3) integer external,
• deptname POSITION(4:14) CHAR)
11/17/20 84
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
SQL*Loader Control File
• OPTIONS (SKIP=1)
• CHARACTERSET UTF16
• Load data
• INFILE *
• APPEND into table XXBEL.XXMTL_ITEM_SUBINV_INTERFACE
• FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '*' TRAILING NULLCOLS
• ( INVENTORY_ITEM_NAME CHAR "TRIM (:INVENTORY_ITEM_NAME)",
• ORGANIZATION_CODE CHAR "TRIM (:ORGANIZATION_CODE)",
• SECONDARY_INVENTORY CHAR "TRIM (:SECONDARY_INVENTORY)",
• INVENTORY_PLANNING_CODE INTEGER EXTERNAL "2",
• PREPROCESSING_LEAD_TIME INTEGER EXTERNAL "TRIM (:PREPROCESSING_LEAD_TIME)",
• PROCESSING_LEAD_TIME INTEGER EXTERNAL "TRIM (:PROCESSING_LEAD_TIME)",
• POSTPROCESSING_LEAD_TIME INTEGER EXTERNAL "TRIM (:POSTPROCESSING_LEAD_TIME)",
• ATTRIBUTE1 CHAR "TRIM (:ATTRIBUTE1)",
• ATTRIBUTE2 CHAR "TRIM (:ATTRIBUTE2)",
• ATTRIBUTE3 CHAR "TRIM (:ATTRIBUTE3)",
• ATTRIBUTE4 CHAR "TRIM (:ATTRIBUTE4)",
• ATTRIBUTE5 CHAR "TRIM (:ATTRIBUTE5)",
• ATTRIBUTE6 CHAR "TRIM (:ATTRIBUTE6)",
• DELETE_FLAG CHAR "TRIM (:DELETE_FLAG)",
• PROCESS_FLAG INTEGER EXTERNAL "20",
• UPLOAD_RECORD_ID sequence(max,1) )
11/17/20 85
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Invoking SQL*Loader
• SQLLDR CONTROL=foo.ctl, LOG=bar.log, BAD=baz.bad, DATA=etc.dat
USERID=scott/tiger, ERRORS=999, LOAD=2000, DISCARD=toss.dis,
DISCARDMAX=5
11/17/20 86
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Invoking SQL*Loader
• SQLLDR CONTROL=foo.ctl, LOG=bar.log, BAD=baz.bad, DATA=etc.dat
USERID=scott/tiger, ERRORS=999, LOAD=2000, DISCARD=toss.dis,
DISCARDMAX=5
11/17/20 87
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Call Java Stored
Procedure from
PLSQL Procedure
11/17/20 88
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Call a Java SP from PL/SQL Procedure
1. Define a Java Stored Stored procedure as below in the database.
DROP JAVA SOURCE APPS."DirList";
CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED apps."DirList" AS import java.io.*;
import java.sql.*;
public class dirlist
{
public static void getlist(string directory)
throws sqlexception
{
try
{
file path = new file( directory );
string[] list = path.list();
string element;
for(int i = 0; i < list.length; i++)
{
element = list[i];
#sql { insert into xx_dir_list (filename)
values (:element) };
} catch (exception e);
{
system.out.ptintln(e.getmeaasage);
}
}
} 11/17/20 89
}
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Call a Java SP from PL/SQL Procedure
2. Call the above Java Stored Stored procedure "DirList" from the PL/SQL
Procedure as follows.
11/17/20 90
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Dynamic SQL in
PL/SQL
11/17/20 91
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Dynamic SQL
11/17/20 92
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Methods for using Dynamic SQL within PL/SQL
Oracle provides two methods for using dynamic SQL within PL/SQL
DBMS_SQL package
11/17/20 93
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Referencing Database Objects that Do Not Exist at Compilation
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Executing Dynamic PL/SQL Blocks
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Dynamic SQL using DBMS_SQL Package
DBMS_SQL.CLOSE_CURSOR (cursor_handle);
END create_index;
/ 11/17/20 96
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
PL/SQL
Performance
Tuning
11/17/20 97
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Make SQL Statements as Efficient as Possible
• Rewrite the SQL with query hints to avoid full table scan
11/17/20 98
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Make Function Calls as Efficient as Possible
BEGIN
-- Inefficient, calls my_function for every row.
FOR item IN (SELECT DISTINCT(SQRT(department_id)) col_alias FROM
employees)
LOOP
dbms_output.put_line(item.col_alias);
END LOOP;
-- Efficient, only calls function once for each distinct value.
FOR item IN ( SELECT SQRT(department_id) col_alias FROM ( SELECT
DISTINCT department_id FROM employees) )
LOOP
dbms_output.put_line(item.col_alias);
END LOOP;
END;
/ 11/17/20 99
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Minimize Datatype Conversions
DECLARE
n NUMBER;
c CHAR(5);
BEGIN
n := n + 15; -- converted implicitly; slow
n := n + 15.0; -- not converted; fast
c := 25; -- converted implicitly; slow
c := TO_CHAR(25); -- converted explicitly; still slow
c := '25'; -- not converted; fast
END;
/
11/17/20 100
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Improving performance by using better Data types
11/17/20 101
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Using the FORALL Statement
This FORALL statement sends all three DELETE statements to the SQL
engine at once:
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Compare performance of FORALL Statement
11/17/20 103
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Tuning PL/SQL Performance with the RETURNING Clause
• Eliminates the need to SELECT the row after an insert or update, or before a
delete. As a result, fewer network round trips, less server CPU time, fewer
cursors, and less server memory are required.
• Example, you update the salary of an employee and at the same time retrieve the
employee's name and new salary into PL/SQL variables.
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
PL/SQL
Wrapping
11/17/20 105
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.
Wrapping PL/SQL Source Code
You can wrap PL/SQL source code with either the wrap utility or DBMS_DDL
subprograms.
Wrap utility
wrap iname=input_file [ oname=output_file ]
DBMS_DDL.WRAP
11/17/20 106
www.intelligroup.com | Copyright © 2010 Intelligroup, Inc. | | PRIVATE AND CONFIDENTIAL | INTELLIGROUP proprietary.