PLSQL Variables - Introduction
PLSQL Variables - Introduction
Topics
- PLSQL – Variables - INTRODUCTION
Introduction to PLSQL Variables
- Declaring Variables
- Initializing Variables
- Scope & Visibility
- PLSQL Data Types
- Q&A
PL/SQL Variables - Declaring Variables
• A variable can be declared only in the declarative part of any block, subprogram, or package.
•A variable declaration always specifies the name and data type of the variable
•It should start with an Alphabet or _
•It can consist of Alphabets, Numbers and # $ & _
•It cannot be PLSQL/SQL Keywords
•It is case insensitive
•It can be of at-most 128 Characters
•As per EvobrixX standards, we need to prefix variable with v_, p_, g_
•For most data types, we can also specify initial value (Or Default)
•PL/SQL data type includes SQL Datatypes also
PL/SQL Variables – Declaring Constants
• %TYPE allows us to declare a variable of same data type as a previously declared variable or
column
• %ROWTYPE allows us to declare a variable of same data types as a row in a table (all columns)
• Reference variables using following modes
• Simple name :Just variable name
• Qualified Name: Unit.variableName (If available in a package)
• Remote Qualified Name: Unit.variableName@remoteDBLink (If available in different Database & package)
PL/SQL Variables – Initializing
• Every PL/SQL constant, variable, parameter, and function return value has a data type that
determines its storage format and its valid values and operations.
• PL/SQL has two kinds of data types: scalar and composite
• Scalar types are types that store single values such as number, Boolean, character, and
datetime
• Composite types are types that store multiple values, for example, record and collection.
• Scalar datatypes are divided into four families
• NUMBER
• BOOLEAN
• CHARACTER
• DATETIME
• PL/SQL scalar data types include SQL data types and its own data type such as Boolean
PL/SQL Variables – Datatypes summary
PL/SQL Variables – Frequently Used Datatypes
• The VARCHAR2 data type stores alphanumeric values in variable-length strings. This means, unlike the CHAR data
type, the length is variable.
• When you create a column with the VARCHAR2 data type, you specify a maximum length between 1 and 4000
bytes. Only the characters in the value you insert or update are stored.
• Using a VARCHAR2 column will often save space when compared to CHAR columns.
• The NVARCHAR2 is similar to the VARCHAR2 data type
• NVARCHAR2 stores Unicode data and allows multi-byte characters to be stored in a variable-length field.
Because PLS_INTEGER datatype uses hardware arithmetic, they are faster than NUMBER operations, which uses software arithmetic.
Because PLS_INTEGER datatype uses hardware arithmetic, they are faster than NUMBER operations, which uses software arithmetic.
• The Number Datatype can store positive numbers, and negative numbers, and can store up to 38 digits.
• When defining a number, you specify two things: a precision and a scale.
• Precision is the total number of digits in a number.
• Scale is the number of digits to the right of the decimal point.
• If the precision is not specified, then the column stores the numbers that are inserted as they are provided.
• If the scale is not specified, then the scale is 0
Because PLS_INTEGER datatype uses hardware arithmetic, they are faster than NUMBER operations, which uses software arithmetic.
• BOOLEAN values are used to store true or false. They work in a similar way to many other
programming languages..
• The PLS_INTEGER data type is only available to PL/SQL code. It works in a similar way to
the NUMBER value in Oracle SQL.
• If you’re using PL/SQL, you can use this data type. They perform better and require less
storage than NUMBER data types.
PL/SQL Variables – Date Data Types
• The DATE data type stores a point in time and includes both the date and time component.
• It stores:
• The year (including century)
• Month
• Day
• Hours
• Minutes
• Seconds
• The default input and output format of a DATE value is DD-MON-YY
• This default format comes from the parameter NLS_DATE_FORMAT. It can be changed during the user session with ALTER SESSION
• The TIMESTAMP data type is very similar to the DATE data type, except that it allows you to store fractions of a second.
PL/SQL Variables – LOB
PL/SQL Variables – BLOB
PL/SQL Variables – CLOB
PL/SQL Variables – Records
A record is a data structure that can hold data items of different kinds. Records consist of different fields, similar to
a row of a database table.
PL/SQL Variables – Records – User Defined
PL/SQL Variables
Q&A