Upload
vidya-sagar
View
221
Download
0
Embed Size (px)
Citation preview
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 2/22
PL/SQL-1 2
CMC
Objectives
• Write your own PL/SQL code using conditional
statements and loop constructs
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 3/22
PL/SQL-1 3
CMC
Introduction
• SQL does not have any procedural capabilities such as
looping and branching nor does it have any conditional
checking capabilities vital for data testing before
storage.
• PL/SQL adds to the power of SQL and provides the
user with all the facilities of a programming
environment.
• PL/SQL bridges the gap between database technology
and procedural programming languages.
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 4/22
PL/SQL-1 4
CMC
• Via PL/SQL you can insert, delete, update and retrieve
table data as well as use procedural techniques such aswriting loops or branching to another block of code.
• PL/SQL is really an extension of SQL.
• PL/SQL blocks can contain any number of SQL
statements. It allows you to logically group a number of
SQL statements and pass them to the DBA as a single
block.
• Without PL/SQL, DBA has to process SQL statements
one at a time.
Introduction contd..
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 5/22
PL/SQL-1 5
CMC
PL/SQL Block
• A standard PL/SQL code segment is called a block. Astandard PL/SQL block is made up of three sections.
1. Declaration Section is meant for declaring variables
and constants.
2. Executable Statements Section is meant for
manipulating objects during execution.
3. Exception Handling Section - any errors or
exceptions raised during execution of the PL/SQL
block are handled here.
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 6/22
PL/SQL-1 6
CMC
PL/SQL Engine
• The PL/SQL Engine accepts the procedural requests
and database queries from the PL/SQL code block andtransfers them to the Procedural Statement Executer
and the SQL Statement Executer respectively.
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 7/22
PL/SQL-1 7CMC
Advantages of PL/SQL
• Procedural Capabilities: Offers constructs for
conditional selection, iteration etc. Variable and
constant declarations are possible.
• Improved Performance: Processing is fast since entireblock is sent to RDBMS rather than one statement at a
time.
• Enhanced Productivity: PL/SQL can be embedded inORACLE application development tools and 3 GL
interface program.
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 8/22
PL/SQL-1 8CMC
• Portability: An application developed in PL/SQL is
portable across h/w and o.s. platforms.
• Integration with RDBMS: Variables in PL/SQL can
belong to any SQL datatype such as - CHAR, DATE
etc.
Advantages of PL/SQL contd..
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 9/22
PL/SQL-1 9CMC
Variables and Constants
• A PL/SQL variable can be used either in SQL or
PL/SQL statements provided it is declared beforereferencing it.
Data Types:
• BINARY_INTEGER Type: You use the
BINARY_INTEGER datatype to store signed integers.
Variables
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 10/22
PL/SQL-1 10CMC
• NUMBER Type: You use the NUMBER datatype to
store fixed or floating point numbers of virtually any size.
• CHAR Type: You use the CHAR datatype to store
fixed-length character data.
• VARCHAR2 Type: You use the VARCHAR2 datatype
to store variable-length character data.
• LONG Type: You use the LONG datatype to store
variable-length character strings.
Variables contd..
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 11/22
PL/SQL-1 11CMC
• RAW Type: You use the RAW datatype to store binary
data or byte strings.
• LONG RAW Type: You use the LONG RAW datatype
to store binary data or byte strings.
• BOOLEAN Type: You use the BOOLEAN datatype to
store the values TRUE and FALSE and the non-value
NULL.
• DATE Type: You use the DATE datatype to store fixed-length date values. The DATE datatype takes no
parameters.
Variables contd..
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 12/22
PL/SQL-1 12CMC
• ROWID Type: Each row in the database has an
address of the type Data Object Number.Relative File
Number.Block Number.Slot Number
• MLSLABEL Type: With Trusted Oracle, you use the
MLSLABEL datatype to store variable- length, binary
operating system labels. Trusted Oracle uses labels to
control access to data.
Variables contd..
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 13/22
PL/SQL-1 13CMC
Constants
• Declaration of a constant is similar to the declaration of a variable except that the keyword CONSTANT must be
added and a value must be assigned.
Variables and Constants contd..
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 14/22
PL/SQL-1 14CMC
Declaration Using Attributes
• This attribute provides the data type of a variable,
constant or column.
• %TYPE attribute is useful while declaring a variable that
has same data type as a table column.
%TYPE attribute
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 15/22
PL/SQL-1 15CMC
%ROWTYPE attribute
• This attribute is used to declare a variable to be a
record having the same structure as a row in a table.
• The row is represented as a record and its fields have
the same names and data types as the columns in the
table or view.
Declaration Using Attributes contd..
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 16/22
PL/SQL-1 16CMC
Operators
Logical:
• AND• OR• NOT
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 17/22
PL/SQL-1 17CMC
Conditional Control
if (condition) then if (condition) then
statements statements
else elsif (condition) thenstatements statements
end if; else
statements
end if;
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 18/22
PL/SQL-1 18CMC
Iterative Control
1. Endless Loop
LOOPstatements
..........
END LOOP;
LOOP and END LOOP are the keywords.
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 19/22
PL/SQL-1 19CMC
Iterative Control contd..
2. For Loop
FOR i IN 1..10
LOOP
statements.........
END LOOP;
- FOR loop index is implicitly declared to be of datatype NUMBER.
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 20/22
PL/SQL-1 20CMC
Iterative Control contd..
3. While Loop
WHILE (condition)
LOOP
.........
statements
.........
END LOOP;
8/2/2019 10. PL SQL - I
http://slidepdf.com/reader/full/10-pl-sql-i 21/22
PL/SQL-1 21CMC
GOTO Statement• The GOTO statement changes the flow of control
within the PL/SQL block.
• GOTO statement is used in combination with label-
names.
• Labels are used as markers before the block or
executable statements to which control is to betransferred.