0% found this document useful (0 votes)
3 views

PLSQL Variables - Introduction

The document provides an introduction to PL/SQL variables, covering their declaration, initialization, scope, visibility, and data types. It details the rules for naming variables, the distinction between variables and constants, and the various PL/SQL data types including scalar and composite types. Additionally, it discusses frequently used data types such as VARCHAR2, NUMBER, and BOOLEAN, along with their characteristics and storage formats.

Uploaded by

ssuresh2107
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PLSQL Variables - Introduction

The document provides an introduction to PL/SQL variables, covering their declaration, initialization, scope, visibility, and data types. It details the rules for naming variables, the distinction between variables and constants, and the various PL/SQL data types including scalar and composite types. Additionally, it discusses frequently used data types such as VARCHAR2, NUMBER, and BOOLEAN, along with their characteristics and storage formats.

Uploaded by

ssuresh2107
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

PL/SQL 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

• A constant holds a value that does not change.


•Same as declaring variables, but has two additional requirements
• The Keyword Constant
• Initial Value
•Changing a constants value in program will throw RTE
PL/SQL Variables – More on Declaration

• %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

• During declaration, initializing variable is optional (UNLESS WE SPECIFY NOT NULL)


•During declaration of Constant, Initializing is NOT optional
•If we do not initialize by default variable is initialized with NULL
• Any operation done on this will result in NULL only
•To initialize we can use the following methods
• Assignment Operator :=
• DEFAULT keyword
• SELECT INTO from table
• Pass as IN OUT/OUT parameter from sub program
PL/SQL Variables – Scope and Visibility

• References to an identifier are resolved according to its scope and visibility.


• Scope is the region of a program where variable can be accessed
• Visibility is the region where the variable can be accessed without qualifying it
• Identifiers declared in a PL/SQL block are considered local to that block and global to all its
sub-blocks.
• If a global identifier is redeclared in a sub-block, both identifiers remain in scope. Within
the sub-block, however, only the local identifier is visible because you must use a qualified
name to reference the global identifier.
• One can qualify variable with its respective block using Labels <<LabelName>>
• Labelname.variableName
• We can qualify using the procedure Name also apart from Label Name
• We cannot have duplicate variables in same appliable scope
PL/SQL Variables – Scope and Visibility (..contd)
PL/SQL Variables – PL/SQL Datatypes

• 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

• Character : VARCHAR2, CHAR


• Numeric : NUMBER, INT, DECIMAL, PLS_INTEGER, BYTE
• Date : DATE
• Boolean : Boolean
• LOB : CLOB
• Composite: Record
Because PLS_INTEGER datatype uses hardware arithmetic, they are faster than NUMBER operations, which uses software arithmetic.

PL/SQL Variables – Character Data Types

• 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.

PL/SQL Variables – Numeric Data Types


PL/SQL Variables – Number Data Type

• 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.

PL/SQL Variables – Other Numeric Data Types

• 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

You might also like