View
233
Download
1
Category
Preview:
Citation preview
Database Application Development using
PL/SQL Programming
PL/SQL Programming
Chapter 1. Introduction
PL/SQL Programming
• 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.
Introduction to PL/SQL
PL/SQL Programming
PL/SQL
block
PL/SQL Environment
PL/SQLblock
ProceduralStatement executor
PL/SQL Engine
SQL statement executor
Oracle server
PL/SQL Programming
PL/SQL and Oracle-based Applications
Oracle applications are designed for client-server architecture.
PL/SQL Programming
• Design and creation of database;• Creation of database triggers;• Building complex business rules at the
database level;• Building screens, reports for user interface
(Objects in these applications may require PL/SQL codes)
Programming in Oracle Applications
PL/SQL Programming
Benefits: Integration Improved performance Modularised Program Development Portability Control Structures Error Handling
PL/SQL Programming
Chapter 2. Declaring Variables
PL/SQL Programming
Recognise the basic PL/SQL block and its sections
Describe the significance of variables in PL/SQL
Distinguish between PL/SQL and non-PL/SQL variables
Declare PL/SQL variables
Execute PL/SQL blocks
Objectives
PL/SQL Programming
DECLARE (Optional)constantsvariablescursorsuser-defined exceptions
BEGIN (Mandatory)SQL statementsPL/SQL statements
EXCEPTION (Optional)Exception handling
END; (Mandatory)
PL/SQL Block Structure
PL/SQL Programming
PL/SQL Block Structure
•PL/SQL Block can have a header that goes on top of the block;
•The header specifies whether the block is a procedure, a function or a package;
• If the header is absent, the block is said to be an anonymous block.
<Block Header>DECLARE (Optional)
constantsvariablescursorsuser-defined
exceptionsBEGIN (Mandatory)
SQL statementsPL/SQL statements
EXCEPTION (Optional)Exception handling
END; (Mandatory)
PL/SQL Programming
DECLAREv_variable VARCHAR2(5);
BEGINSELECT column_nameINTO v_variableFROM table_name;
EXCEPTIONWHEN exception_name THEN...
END; /
Executing Statements and PL/SQL Blocks
PL/SQL Programming
[DECLARE]
BEGIN
-- statements
[EXCEPTION]
END;
Block Types
PROCEDURE name
IS
BEGIN
-- statements
[EXCEPTION]
END;
FUNCTION name
RETURN datatype
IS
BEGIN
--statements
RETURN value;
[EXCEPTION]
END;
Anonymous Procedure Function
PL/SQL Programming
Anonymous blocks
Stored procedures or functions
Application procedures or functions
Packages (Application or Stored) Database triggers (triggered by DML
statements)
Application triggers
Program Constructs
PL/SQL Programming
Variables can be used for:
Temporary storage of data Manipulation of stored values Reusability Ease of maintenance
Use of variables
PL/SQL Programming
Declare and initialise variables within the declarative section
Assign new values to variables within the executable section
Pass values into PL/SQL blocks through parameters
View results through output variables
Handling variables in PL/SQL
PL/SQL Programming
PL/SQL variables Scalar Composite Reference LOB (Large objects)
Non-PL/SQL variables Bind and host variables
Types of variables
PL/SQL Programming
TRUE represents a Boolean value. 30-MAY-78 represents a DATE. photo represents a LOB. text of a speech represents a LONG. 8523.25 represents a NUMBER. Movie represents a BFILE. “Dhaka” represents a VARCHAR2.
Examples
PL/SQL Programming
Syntax:
identifier [CONSTANT] datatype [NOT NULL]
[:= | DEFAULT expr];
Examples:
v_hire_date DATE;
v_department_id NUMBER(4) NOT NULL := 10;
v_city VARCHAR2(30) := ‘Sylhet’;
c_commission_pct CONSTANT NUMBER := 1400;
Declaring PL/SQL Variables
PL/SQL Programming
• Follow a naming convention.
• Initialise variables designated as NOT NULL and CONSTANT.
• Declare one identifier per line.
• Initialise identifiers by using the assignment operator (:=) or the DEFAULT reserved word.
identifier := expr;
Guidelines for Declaring PL/SQL Variables
PL/SQL Programming
• Two variables can have the same name, provided they are in different blocks.
• The variable name should not be the same as the name of columns of the table(s) used in the block.
• Adopt a naming convention for identifiers.For example: v_variable for a variable;
c_constant for a constant; g_message for a global (or host)
variable;
• An identifier must not be longer than 30 characters.
Naming Rules
PL/SQL Programming
DECLARE
employee_id NUMBER(6);
BEGIN
FROM employees
WHERE last_name = ‘Smith’;
END;
Naming Rules
SELECT employee_id
INTO employee_idBetter to avoid
PL/SQL Programming
• Assignment operator (:=)• DEFAULT keyword• NOT NULL constraint
Syntax:identifier := expr;
Examples:v_hiredate := ’31-DEC-98’ ;v_mgr NUMBER(6) DEFAULT 100;v_name := ‘King’;
Variable Initialization and Keywords
PL/SQL Programming
• Hold a single value• Have no internal components• Classified into four categories (they may have
sub-types)
-> number -> character -> date -> boolean
Scalar Data Types
PL/SQL Programming
• CHAR [(maximum_length)]• VARCHAR2 [(maximum_length)]• LONG• LONG RAW• NUMBER [(precision, scale)]• BINARY_INTEGER• PLS_INTEGER• BOOLEAN
Base Scalar Data Types
PL/SQL Programming
• DATE• TIMESTAMP• TIMESTAMP WITH TIME ZONE• TIMESTAMP WITH LOCAL TIME ZONE• INTERVAL YEAR TO MONTH• INTERVAL DAY TO SECOND
Base Scalar Data Types
PL/SQL Programming
DECLAREv_job VARCHAR2 (9);v_count BINARY_INTEGER := 0;v_total_sal NUMBER (9,2) := 0;v_orderdate DATE := SYSDATE + 7;c_tax_rate CONSTANT NUMBER (3,2) :=
8.25;v_valid BOOLEAN NOT NULL := TRUE;…
Scalar Variable Declarations
PL/SQL Programming
• Declare a variable according to: A database column definition Another previously declared variable
• Prefix %TYPE with: The database table and column The previously declared variable name
The %TYPE Attribute
PL/SQL Programming
Syntax:identifier Table.column_name%TYPE;
Examples:
…v_name employees.last_name%TYPE;
v_balance NUMBER(7,2);
v_min_balance v_balance%TYPE := 10;…
Declaring Variables with the %TYPE Attribute
PL/SQL Programming
• Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable.
• The variables are compared by the logical operators AND, OR, and NOT.
• The variables always yield TRUE, FALSE, or NULL.• Arithmetic, character, and date expressions can be used
to return a Boolean value.
Declaring Boolean Variables
v_sal1 := 50000;
v_sal2 := 60000;
v_sal1 < v_sal2
DECLARE
v_flag BOOLEAN := FALSE;
BEGIN
v_flag := TRUE;
END;
PL/SQL Programming
• PL/SQL TABLES• PL/SQL RECORDS
Composite Datatypes
PL/SQL Programming
CLOB (character large object)
– stores large blocks of single-byte character data (Book)
BLOB (binary large object)
– stores large binary objects (Photo)
BFILE (binary file)
– stores large binary objects in operating system files (Movie)
NCLOB (national language character large object)
– stores unicode data
LOB Data Type Variables
PL/SQL Programming
• A variable declared in a host environment.• Used to pass run-time values (character or number)
How to create? In SQL*Plus environment -
VARIABLE return_code NUMBER
VARIABLE return_message VARCHAR2(30)
How to reference in PL/SQL Block?Needs to be preceded by a colon.
:bind_variableHow to display?
PRINT bind_variable
Bind Variables
PL/SQL Programming
Example:
In SQL*Plus prompt, declare-
VARIABLE g_salary NUMBER
Using Bind Variables
BEGINSELECT salaryINTO :g_salaryFROM employeesWHERE employee_id = 178;
END;
PL/SQL Programming
• Oracle-supplied packaged procedure• An alternative for displaying data from a PL/SQL block• Needs to be enabled with SET SERVEROUTPUT ON
DBMS_OUTPUT.PUT_LINE
SET SERVEROUTPUT ONDEFINE p_annual_sal = 60000;DECLARE
v_sal NUMBER(9,2) := &p_annual_sal;BEGIN
v_sal := v_sal/12;DBMS_OUTPUT.PUT_LINE (‘Monthly salary
is ‘ || TO_CHAR(v_sal));END;
PL/SQL Programming
Time for
Practice 2
Recommended