24893666-PL-SQL-Les-01 (1)
Transcript of 24893666-PL-SQL-Les-01 (1)
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
1/38
1Copyright Oracle Corporation, 2001. All rights
Declaring Variables
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
2/38
Copyright Oracle Corporation, 2001. All rights1-2
ObjectivesObjectives
After completing this lesson, you should be able todo the following:
Recognize the basic PL/SQL block and its sections
Describe the significance of variables in PL/SQL Declare PL/SQL variables
Execute a PL/SQL block
After completing this lesson, you should be able todo the following:
Recognize the basic PL/SQL block and its sections
Describe the significance of variables in PL/SQL Declare PL/SQL variables
Execute a PL/SQL block
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
3/38
Copyright Oracle Corporation, 2001. All rights1-3
PL/SQL Block StructurePL/SQL Block Structure
DECLARE (Optional)Variables, cursors, user-defined exceptions
BEGIN (Mandatory)
SQL statements
PL/SQL statements
EXCEPTION (Optional)
Actions to perform when errors occur
END; (Mandatory)
DECLARE
BEGIN
END;
EXCEPTION
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
4/38
Copyright Oracle Corporation, 2001. All rights1-4
Executing Statements and PL/SQL BlocksExecuting Statements and PL/SQL Blocks
DECLARE
v_variable VARCHAR2(5);BEGIN
SELECT column_name
INTO v_variable
FROM table_name;
EXCEPTION
WHEN exception_name THEN
...
END;
DECLARE
BEGIN
END;
EXCEPTION
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
5/38
Copyright Oracle Corporation, 2001. All rights1-5
Block TypesBlock Types
Anonymous Procedure Function
[DECLARE]
BEGIN--statements
[EXCEPTION]
END;
PROCEDURE nameIS
BEGIN--statements
[EXCEPTION]
END;
FUNCTION nameRETURN datatype
ISBEGIN--statementsRETURN value;
[EXCEPTION]
END;
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
6/38
Copyright Oracle Corporation, 2001. All rights1-6
Program ConstructsProgram Constructs
DECLARE
BEGIN
END;
EXCEPTION
Tools ConstructsAnonymous blocks
Application procedures or
functions
Application packagesApplication triggers
Object types
Database Server
ConstructsAnonymous blocks
Stored procedures or
functionsStored packages
Database triggers
Object types
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
7/38Copyright Oracle Corporation, 2001. All rights1-7
Use of VariablesUse of Variables
Variables can be used for:
Temporary storage of data
Manipulation of stored values
Reusability
Ease of maintenance
Variables can be used for:
Temporary storage of data
Manipulation of stored values
Reusability
Ease of maintenance
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
8/38Copyright Oracle Corporation, 2001. All rights1-8
Handling Variables in PL/SQLHandling Variables in PL/SQL
Declare and initialize variables in the declarationsection.
Assign new values to variables in the executable
section. Pass values into PL/SQL blocks through
parameters.
View results through output variables.
Declare and initialize variables in the declarationsection.
Assign new values to variables in the executable
section. Pass values into PL/SQL blocks through
parameters.
View results through output variables.
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
9/38Copyright Oracle Corporation, 2001. All rights1-9
Types of VariablesTypes of Variables
PL/SQL variables: Scalar
Composite
Reference LOB (large objects)
Non-PL/SQL variables: Bind and host variables
PL/SQL variables: Scalar
Composite
Reference LOB (large objects)
Non-PL/SQL variables: Bind and host variables
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
10/38Copyright Oracle Corporation, 2001. All rights1-10
Using iSQL*Plus Variables Within PL/SQL
Blocks
Using iSQL*Plus Variables Within PL/SQL
Blocks
PL/SQL does not have input or output capability ofits own.
You can reference substitution variables within aPL/SQL block with a preceding ampersand.
iSQL*Plus host (or bind) variables can be used topass run time values out of the PL/SQL block backto theiSQL*Plus environment.
PL/SQL does not have input or output capability ofits own.
You can reference substitution variables within aPL/SQL block with a preceding ampersand.
iSQL*Plus host (or bind) variables can be used topass run time values out of the PL/SQL block backto theiSQL*Plus environment.
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
11/38Copyright Oracle Corporation, 2001. All rights1-11
TRUETRUE
Types of VariablesTypes of Variables
25-JAN-0125-JAN-01
AtlantaAtlanta
Four score and seven years ago
our fathers brought forth uponthis continent, a new nation,
conceived in LIBERTY, and dedicated
to the proposition that all men
are created equal.256120.08256120.08
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
12/38Copyright Oracle Corporation, 2001. All rights1-12
Declaring PL/SQL VariablesDeclaring PL/SQL Variables
Syntax:
Examples:
Syntax:
Examples:
identifier[CONSTANT] datatype [NOT NULL][:= | DEFAULT expr];
DECLAREv_hiredate DATE;
v_deptno NUMBER(2) NOT NULL := 10;v_location VARCHAR2(13) := 'Atlanta';c_comm CONSTANT NUMBER := 1400;
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
13/38Copyright Oracle Corporation, 2001. All rights1-13
Guidelines for Declaring PL/SQL VariablesGuidelines for Declaring PL/SQL Variables
Follow naming conventions.
Initialize variables designated as NOT NULL andCONSTANT.
Declare one identifier per line.
Initialize identifiers by using the assignmentoperator (:=) or the DEFAULT reserved word.
Follow naming conventions.
Initialize variables designated as NOT NULL andCONSTANT.
Declare one identifier per line.
Initialize identifiers by using the assignmentoperator (:=) or the DEFAULT reserved word.
identifier:= expr;
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
14/38Copyright Oracle Corporation, 2001. All rights1-14
Naming RulesNaming Rules
Two variables can have the same name, provided theyare in different blocks.
The variable name (identifier) should not be the sameas the name of table columns used in the block.
Two variables can have the same name, provided theyare in different blocks.
The variable name (identifier) should not be the sameas the name of table columns used in the block.
DECLAREemployee_id NUMBER(6);
BEGINSELECT employee_idINTO employee_id
FROM employees
WHERE last_name = 'Kochhar';END;/
Adopt a naming
convention for
PL/SQL identifiers:
for example,
v_employee_id
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
15/38Copyright Oracle Corporation, 2001. All rights1-15
Variable Initialization and KeywordsVariable Initialization and Keywords
Assignment operator (:=)
DEFAULT keyword
NOT NULL constraint
Syntax:
Examples:
Assignment operator (:=)
DEFAULT keyword
NOT NULL constraint
Syntax:
Examples:
identifier:= expr;
v_hiredate := '01-JAN-2001';
v_ename := 'Maduro';
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
16/38
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
17/38Copyright Oracle Corporation, 2001. All rights1-17
Scalar Data TypesScalar Data Types
Hold a single value
Have no internal components
Hold a single value
Have no internal components
25-OCT-9925-OCT-99
AtlantaAtlanta
Four score and seven years
ago our fathers brought
forth upon this continent, a
new nation, conceived in
LIBERTY, and dedicated to
the proposition that all men
are created equal.
TRUETRUE
256120.08256120.08
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
18/38Copyright Oracle Corporation, 2001. All rights1-18
Base Scalar Data TypesBase Scalar Data Types
CHAR [(maximum_length)]
VARCHAR2(maximum_length)
LONG
LONG RAW
NUMBER [(precision, scale)]
BINARY_INTEGER
PLS_INTEGER
BOOLEAN
CHAR [(maximum_length)]
VARCHAR2(maximum_length)
LONG
LONG RAW
NUMBER [(precision, scale)]
BINARY_INTEGER
PLS_INTEGER
BOOLEAN
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
19/38Copyright Oracle Corporation, 2001. All rights1-19
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
20/38Copyright Oracle Corporation, 2001. All rights1-20
Base Scalar Data TypesBase Scalar Data Types
DATE
TIMESTAMP
TIMESTAMP WITH TIME ZONE
TIMESTAMP WITH LOCAL TIME ZONE
INTERVAL YEAR TO MONTH
INTERVAL DAY TO SECOND
DATE
TIMESTAMP
TIMESTAMP WITH TIME ZONE
TIMESTAMP WITH LOCAL TIME ZONE
INTERVAL YEAR TO MONTH
INTERVAL DAY TO SECOND
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
21/38
Copyright Oracle Corporation, 2001. All rights1-21
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
22/38
Copyright Oracle Corporation, 2001. All rights1-22
Scalar Variable DeclarationsScalar Variable Declarations
DECLARE
v_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;
...
Examples:Examples:
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
23/38
Copyright Oracle Corporation, 2001. All rights1-23
The %TYPE AttributeThe %TYPE Attribute
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
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
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
24/38
Copyright Oracle Corporation, 2001. All rights1-24
Declaring Variables
with the %TYPE Attribute
Declaring Variables
with the %TYPE Attribute
Examples:Examples:
...v_name employees.last_name%TYPE;v_balance NUMBER(7,2);
v_min_balance v_balance%TYPE := 10;...
identifier Table.column_name%TYPE;
Syntax:Syntax:
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
25/38
Copyright Oracle Corporation, 2001. All rights1-25
Declaring Boolean VariablesDeclaring Boolean Variables
Only the values TRUE, FALSE, and NULL can beassigned to a Boolean variable.
The variables are compared by the logical
operators AND, OR, and NOT. The variables always yield TRUE, FALSE, orNULL.
Arithmetic, character, and date expressions can beused to return a Boolean value.
Only the values TRUE, FALSE, and NULL can beassigned to a Boolean variable.
The variables are compared by the logical
operators AND, OR, and NOT. The variables always yield TRUE, FALSE, orNULL.
Arithmetic, character, and date expressions can beused to return a Boolean value.
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
26/38
Copyright Oracle Corporation, 2001. All rights1-26
1 5000
2 2345
3 12
4 3456
1 SMITH
2 JONES
3 NANCY
4 TIM
PL/SQL table structure PL/SQL table structure
BINARY_INTEGER
VARCHAR2
BINARY_INTEGER
NUMBER
Composite Data TypesComposite Data Types
TRUE 23-DEC-98 ATLANTA
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
27/38
Copyright Oracle Corporation, 2001. All rights1-27
LOB Data Type VariablesLOB Data Type Variables
Book(CLOB)
Photo
(BLOB)
Movie
(BFILE)
NCLOB
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
28/38
Copyright Oracle Corporation, 2001. All rights1-28
Bind VariablesBind Variables
Server
O/S
Bind variable
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
29/38
Copyright Oracle Corporation, 2001. All rights1-29
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
30/38
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
31/38
Copyright Oracle Corporation, 2001. All rights1-31
Referencing Non-PL/SQL VariablesReferencing Non-PL/SQL Variables
Store the annual salary into a iSQL*Plus host variable.
Reference non-PL/SQL variables as host variables.
Prefix the references with a colon (:).
Store the annual salary into a iSQL*Plus host variable.
Reference non-PL/SQL variables as host variables.
Prefix the references with a colon (:).
:g_monthly_sal := v_sal / 12;
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
32/38
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
33/38
Copyright Oracle Corporation, 2001. All rights1-33
SummarySummary
In this lesson you should have learned that:
PL/SQL blocks are composed of the followingsections:
Declarative (optional) Executable (required)
Exception handling (optional)
A PL/SQL block can be an anonymous
block, procedure, or function.
In this lesson you should have learned that:
PL/SQL blocks are composed of the followingsections:
Declarative (optional) Executable (required)
Exception handling (optional)
A PL/SQL block can be an anonymousblock, procedure, or function.
DECLARE
BEGIN
END;
EXCEPTION
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
34/38
Copyright Oracle Corporation, 2001. All rights1-34
SummarySummary
In this lesson you should have learned that: PL/SQL identifiers:
Are defined in the declarative section
Can be of scalar, composite, reference, orLOB datatype
Can be based on the structure of another variable ordatabase object
Can be initialized
Variables declared in an external environment suchas iSQL*Plus are called host variables.
Use DBMS_OUTPUT.PUT_LINE to display data froma PL/SQL block.
In this lesson you should have learned that: PL/SQL identifiers:
Are defined in the declarative section
Can be of scalar, composite, reference, orLOB datatype
Can be based on the structure of another variable ordatabase object
Can be initialized
Variables declared in an external environment suchas iSQL*Plus are called host variables.
Use DBMS_OUTPUT.PUT_LINE to display data froma PL/SQL block.
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
35/38
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
36/38
Copyright Oracle Corporation, 2001. All rights1-36
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
37/38
Copyright Oracle Corporation, 2001. All rights1-37
-
8/7/2019 24893666-PL-SQL-Les-01 (1)
38/38