In any programming language, you need
datatypes to be able to manipulate data quickly. PL/SQL provides a variety of datatypes to help you store values. In this module, we will first take a look at the
composition of a PL/SQL block. Then, you will learn about the rules for using a nested block. You will also learn about identifiers and literals that will help you build program logic. Next, we will review the different conversion functions that let you convert datatypes. You will also learn the use of operators in PL/SQL. This will provide you with the knowledge necessary to help build and execute your first PL/SQL block. In this process, we will also review the different ways to comment code.
- Module Objectives
By the end of this module, you will know how to:
- Describe the basic structure of a PL/SQL block
- Define the scope of variables within nested blocks
- Place identifiers and use literals within a PL/SQL block
- Add comments to a PL/SQL block
- Identify implicit and explicit datatype conversions within PL/SQL
- Use operators within PL/SQL
Like many other programming languages, offers a variety of data types to manage and manipulate data efficiently. Here's an overview of some key data types in PL/SQL:
- Numeric Types:
NUMBER: General purpose numeric data type for integers or real numbers.
- Examples:
- NUMBER, NUMBER(5,2) for precision and scale.
- BINARY_FLOAT, BINARY_DOUBLE: Floating-point types for faster numeric operations.
-
Character Types:
- CHAR: Fixed-length character data up to 32767 bytes. Pads with spaces if shorter.
- VARCHAR2: Variable-length character data up to 32767 bytes. Does not pad with spaces.
- NCHAR, NVARCHAR2: Similar to CHAR and VARCHAR2 but for Unicode data.
-
Date and Time Types:
- DATE: Stores date and time in a fixed format.
- TIMESTAMP: Date and time, including fractional seconds.
- TIMESTAMP WITH TIME ZONE: Adds time zone information to TIMESTAMP.
- TIMESTAMP WITH LOCAL TIME ZONE: Converts to the database's local time zone.
-
Large Object (LOB) Types:
- BLOB: Binary Large Object for storing large binary data like images or sounds.
- CLOB: Character Large Object for storing large text data.
- NCLOB: Like CLOB but for Unicode data.
- BFILE: For storing pointers to external binary files.
-
Boolean Type:
- BOOLEAN: Represents true or false values, used primarily in PL/SQL code for control structures.
-
Miscellaneous Types:
- RAW: For raw binary data up to 2000 bytes.
- LONG: An older type for variable-length character data up to 2GB (though VARCHAR2 or LOB types are generally preferred now).
- ROWID: A unique identifier for every row in a table.
-
User-Defined Types:
- PL/SQL Records: Composite data types that can hold multiple data items of different types.
- Collections (Nested Tables, VARRAYs, Associative Arrays): For grouping elements of the same type.
-
Special Types:
- REF CURSOR: Used for dynamic SQL queries where the structure of the result set is not known at compile time.
Here's a simple example to illustrate some of these data types in PL/SQL:
DECLARE
v_number NUMBER(10,2) := 12345.67; -- Numeric type with precision and scale
v_char CHAR(5) := 'Hello'; -- Fixed length character string
v_varchar2 VARCHAR2(20) := 'World'; -- Variable length character string
v_date DATE := SYSDATE; -- Current date
v_boolean BOOLEAN := TRUE; -- Boolean type
v_blob BLOB; -- Binary Large Object (BLOB)
v_clob CLOB; -- Character Large Object (CLOB)
BEGIN
DBMS_OUTPUT.PUT_LINE('Number: ' || v_number);
DBMS_OUTPUT.PUT_LINE('Char: ' || v_char);
DBMS_OUTPUT.PUT_LINE('Varchar2: ' || v_varchar2);
DBMS_OUTPUT.PUT_LINE('Date: ' || v_date);
DBMS_OUTPUT.PUT_LINE('Boolean: ' || CASE WHEN v_boolean THEN 'True' ELSE 'False' END);
END;
/
Each data type in PL/SQL serves specific purposes, allowing for efficient storage, retrieval, and manipulation of different kinds of data within Oracle databases.
PL/SQL combines the data-manipulating power of SQL with the processing power of
procedural languages.
When you can solve a problem with SQL, you can issue SQL statements from your PL/SQL program, without learning new APIs.
Like other procedural programming languages, PL/SQL lets you declare constants and variables, control program flow, define subprograms, and trap runtime errors. You can break complex problems into easily understandable subprograms, which you can reuse in multiple applications.
When it comes to datatypes, PL/SQL supports the standard types.
In the next lesson, the basic structure of a PL/SQL block we will bed described.