PL SQL
PL SQL
BEGIN (MANDATORY)
EXECEPTION (OPTIONAL)
END (MANDATORY)
CONTROL STATEMENT
1) CONDITIONAL CONTROL:
-----------------------
-----------------------
<< Label_Name>>
Statement;
PL/SQL VARIABLES
If needs to declare the variable the variable first in the declaration section of a
PL/SQL Block before using it. By default, variable names are not case section a
reserved PL/SQL keyboard cannot be used as a Variable Name.
Syntax: Variable_Name [constant] Data type [Not Null]
: = [Default initial_value]
CURSOR
Cursor is a Private SQL Area. A cursor is a pointer to the context area used by
the Oracle Engine for Executing SQL Statement.
In PL/SQL Database systems Having two types of Cursors.
1) IMPLICIT CURSOR
2) EXPLICIT CURSOR
1) IMPLICIT CURSORS: The cursors which is Automatically Created, Maintained
and Closed by the oracle engine While Execution of any DML Commands Like
‘Delete, Update, insert’ are called Implicit cursor. Implicit cursors ware controlled by
oracle sever.
Implicit Cursor Having 4 Attributes:
ATTRIBUTES DESCRIPTION
SQL % FOUND It returns values is true, if DML Statements
Like insert, update, delete effect at least one
row or more rows or select into statement
returned one rows otherwise it returns False
SQL % NOT FOUND It returns value is True. If DML statement
like insert, update, delete effect no rows of a
select into statement returns no rows
otherwise if returns false. if is just operate
of SQL % found
SQL % IS OPEN It always returns false for implicit cursor.
Because the SQL cursor is automatically
closed after existing it’s associates SQL
statement.
SQL % ROW COUNT It returns the No. of rows affected by DML
statement’s like insert, update, delete or
returned by a select in to statement.
Cursor Attributes:
CURSOR ATTRIBUTES DESCRIPTION
CURSOR NAME % FOUND It returns the ‘TRUE’. if fetched at least
one record successfully, else it will
return FALSE
CURSOR NAME % NOT FOUND It will return ‘TRUE’. If fetched
operations could not able to fetch any
record.
CURSOR NAME % IS OPEN It returns ‘TRUE’. If the given cursor is
already open, else it it returns FALSE
CURSOR NAME % ROW COUNT It returns the number of records Fetched
from the cursor.
Ref cursor are used to execute No.of select statement dynamically for a single
active set area ref cursor.
(or)
This is a user defined type which is used to process multiple records and also
this is a record-by-record process.
Types of Refcursor: 1) Strong Refcursor
2)Weak Refcursor
1) STRONG REFCURSOR: This is a Refcursor which have return type, where as
weak Refcursor has no return type.
Syntax: (strong ref cursor)
Type TypeName is Refcursor return record type data type;
Variable columnname TypeName;
Strong ref cursor variable name
Syntax: (weak ref cursor)
Type TypeName is Refcursor;
Variable name TypeName;
Weak Refcursor variable name
SYS_REFCURSOR
EXCEPTIONS
3) UNNAMED EXCEPTION: In this method we are creating our own exception name
and they associate this exception name with appropriate error number by using
exception _INIT function this function accepts two parameters.
Syntax: Pragma Exception_INIT (User define Exception name, Error number);
SUB-PROGRAMS
PACKAGES
Package is a database objects, which encapsulates procedures, function, cursor, global
variables, constants types into single unit.
Packages also having two parts:
1) Package Specification
2) Package Body
COLLECTIONS
Collections are most useful, when a large data of the same data type need to be
processed.
Types of Collections:
1) INDEX BY TABLE (OR) PL/SQL TABLE (OR) ASSOCIATIVE ARRAY
Syntax: Type type_name is table of data_type(size) index by Binary Integer ;
(OR)
Type type_name is table of data_type index by varchar2(10);
Variable name type_name;
2) NESTED TABLE
Syntax: Type type_name is table of datatype
Variable name type_name: = type_name ()
3) VARRAY
Syntax: Type Varray_type_name VARRY(N) of (element type);
Variable name type_name: = type_name ()
Collection methods: PL/SQL provides the built-in collection method that make collection
easier to use.
1) EXISTS(N): - Syntax: <collection name> exists (element .Position)
2) COUNT: - Syntax: <collection name> .Count
3) LIMIT: - Syntax: <collection name> .Limit
4) FIRST: - Syntax: <collection name> .First
5) LAST: - Syntax: <collection name> .Last
6) PRIOR(N): - Syntax: <collection name> .Prior(n)
7) NEXT(N): - Syntax: <collection name> .Next(n)
8) EXTEND: - Syntax: <collection name> .Extend(n)
9) EXTEND(N): - Syntax: <collection name> .Extend(n)
10) EXTEND (N, I): - Syntax: <collection name> .Extend(n, i)
11) TRIM: - Syntax: <collection name> .Trim
12) TRIM(N): - Syntax: <collection name> .Trim(n)
13) DELETE: - Syntax: <collection name> .Delete
14) DELETE(N): - Syntax: <collection name> .Delete(n)
15) DELECTE (M, N): - Syntax: <collection name> .Delete(m,n)
TRIGGERS
Triggers is a named PL/SQL data base object. It will automatically be invoked
whenever DML operation performed against table (or) views.
Types Triggers.
1) STATEMENT LEVEL TRIGGER: In STATEMENT level trigger body is executed
only once for DML statement.
2) ROW LEVEL TRIGGER: In ROW level trigger, trigger body is executed by each
row for DML statement.
3) INSTEAD OF TIGGER
4) COMPOUND TRIGGER: In oracle 11G The concept of compound trigger was
introduced A compound trigger is a single trigger on a table that enable you to specify
actions for each of four timing points.
1) Before that firing statement
2) Before each row that the firing statement affects
3) After each row that the firing statement affects
4) After the firing statement
With the compound trigger both the statement level and row level action can be put
up in single trigger.
Syntax: create or replace trigger compound_trigger_name for insert/update/delete of column
on table compound trigger.
5) MUTATING ERROR: Mutating trigger error occurred when trigger referees the
table that owns the trigger resulting in the ORA-04091-Table-Mutating
Syntax: create or replace trigger trigger_name before/after/insert/update/delete on
table_name
For each row
When condition
Declare
Begin
Exception
End;/
UTL_FILE PACKAGE
This package is used to right data into the flat file and also read data from flat file. If
we want to wright data into file then we using “PUTF” (procedure). If you want to read data
from the file the we are using, Get_Line procedure.
Create Directory: Before we are using UTL_FILE Package we must create directory.
Syntax: create or replace directory directory_name as ‘path’;
Grant create any directory to scott;
Grant Read, right on directory directory_name : to user name;
Writing Data in to flat file:
Step 1: Before we are opening the file, we must create a file pointer variable by using
‘File_Type’ from “UTL_FIlE” Package in Declare section of the PL/SQL Block.
Syntax: File_pointer_Variable name Utl_file.file type;
Step2: Before we are writing data into a file then we must open the file by using “FOPEN”
function from “UTL_FILE” package this function accepts 3 paraments.
Syntax: File_point_Varriable name := UTL_FILE.FOPEN(‘Directory name’, ‘File name’,
‘MOD’ );
MOD: Three types (W_write R_right A_appended)
Step3: If we want to right data into file then we are using “PUTF” procedure from “UTL
_FILE” package.
Syntax: Utl_File.putf (file pointer variable name, ‘content’);
Step4: After writing data into file then must close the file by using “FCLOSE” procedure
from “UTL_FILE” package.
Syntax: UTL_FILE.FCLOSE(file pointer variable name);
Read Data from Flat File: If we want to read data from flat files then we must used
Get_Line procedure from “UTL_FILE” package.
Syntax: UTL_FIlE.Get_Line(file pointer variable, Buffer variables);
DYNAMIC SQL
In dynamic SQL, SQL Statement are executed at run time. Generally, in
PL/SQL we are not allowed to use DDL, DCL statement. If we want to use those statements
in PL/SQL then we are using Dynamic SQL. In dynamic SQL SQL statements must be
specified with in single quotes and also those statement is executed by using “execute
immediate” clause. This clause must be used in executable section of begin the PL/SQL
Block.
Syntax: begin
Execute immediate “SQL Statement”;
End;
LOB[LARGE OBJECT]
All database systems having 2 types of Large Objects.
1) INTERNAL LARGE OBJECTS
2) EXTERNAL LARGE OBJECTS
1)INTERNAL LARGE OBJECT:
Internal large objects are stored with in data base.
oracle having 2 types of internal large object
1) C Lob [character large object]
2) B Lob [binary large object]
Syntax: column_name CLob [column_name datatype]
Column_name Blob [column_name datatype]
2)EXTERNAL LARGE OBJECT:
External large object are store in out said of the database.
These objects are stored in operating system files.
This in an Bfile data type.
Syntax: column_name bfile [column_name datatype]
Difference between Long and Lob
LONG LOB
Can store up to 2GB data Can store up to 4GB data
A table contain only 1 long column A table contain more than 1 lob column