0% found this document useful (0 votes)
202 views9 pages

PL/SQL Basics: Variables and Syntax

PL/SQL provides an extension to SQL that allows for procedural logic, variables, conditions, and loops. Key features of PL/SQL include: - Block structure with DECLARE, BEGIN, END that allows variable declarations and executable code. - Support for all standard SQL statements and functions as well as variables of various data types that can be assigned values. - Syntax elements like comments, assignments, constants, and %type that simplify variable declarations linked to database columns.

Uploaded by

bookworm_shashi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
202 views9 pages

PL/SQL Basics: Variables and Syntax

PL/SQL provides an extension to SQL that allows for procedural logic, variables, conditions, and loops. Key features of PL/SQL include: - Block structure with DECLARE, BEGIN, END that allows variable declarations and executable code. - Support for all standard SQL statements and functions as well as variables of various data types that can be assigned values. - Syntax elements like comments, assignments, constants, and %type that simplify variable declarations linked to database columns.

Uploaded by

bookworm_shashi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Support For PL/SQL

• SQL Data Manipulation Statements: SELECT, INSERT,


DELETE, UPDATE
• SQL Transaction Statements: COMMIT, ROLLBACK [TO],
SAVEPOINT, LOCK TABLE
• SQL Functions: Numeric, Character, Date, Group, Date
Conversion
• SQL Predicates: Simple Logic, AND, OR, NOT, BETWEEN,
IS NULL, LIKE, EXISTS, ANY, ALL
PL/SQL Syntax

• PL/SQL commands all terminate with a semicolon.


• PL/SQL statements are coded in free field format.
• Case of the commands is irrevant.
• Comments:
-- Comment
/* Comment */
PL/SQL Syntax (Continued)

Block Definition Syntax


[DECLARE
Block declarations]
BEGIN
Block statements
END;
Blocks may be embedded
PL/SQL Variables

• Variables: Used just like variables in any other language.

• Variable declarations: Can declare a variable of any


datatype native to ORACLE: Number, Varchar2, Char,
Date, etc. or to PL/SQL (Boolean or Record).

• PL/SQL does not support arrays of any type.


PL/SQL Variable Declaration Syntax

Declaration:
identifier datatype [NOT NULL] [:=
plsql_expression] ;
Examples:
bonus NUMBER (11,2); in_stock
BOOLEAN; total NUMBER
(7, 2) := 0.0;
birthday DATE := ‘09-OCT-1945’;
name CHAR (20);
Assigning Values To Variables

Two methods of assignment.


a)Straight assignment statement (:=)
a)tax := price * tax_rate;
b)bonus := salary * 0.10;
b)Assignment through the SELECT statement

SELECT SAL INTO CUR_SAL


FROM EMP
WHERE EID = ‘E12345’;

SELECT SAL * 0.10


INTO CUR_SAL FROM EMP
WHERE EID = ‘E12345’;
Constants

• Similar to variable declaration except the word


CONSTANT is inserted in the declaration and the
constant value is added to the declaration.
• Examples:
bonus_multiplier CONSTANT NUMBER
(3,2) := 0.10;
Declarations Using Types

• %type attribute provides the datatype of a variable,


constant, or column.

• Useful when declaring a variable that refers to a column


in a table

• Example: Assume a column called BOOK_TITLE is an


attribute of a table named BOOK_INFO. Declare a
variable called TITLE to have the same datatype as
BOOK_TITLE without knowing what type BOOK_TITLE is.
Declarations Using Types (continued)

• Example:
title book_info.book_title%type;
• Advantages:
– Don’t need to know BOOK_TITLE type
– If type changes, no problem
Declarations Using Rows

• %Rowtype is used to declare a record with the same


types as each value retrieved in a SELECT command.
• More on this later.

Common questions

Powered by AI

PL/SQL does not support arrays as a native data structure type. Instead, alternatives like associative arrays (also known as index-by tables) can be used, which are essentially key-value pairs that can be indexed by integers or strings. These associative arrays allow for similar functionalities as arrays in other languages, though with differences due to not natively supporting ordered collections .

PL/SQL supports transaction control operations via statements such as COMMIT, ROLLBACK [TO], SAVEPOINT, and LOCK TABLE. Each serves to manage the state and durability of transactions performed on the database. COMMIT permanently saves changes, ROLLBACK undoes them, SAVEPOINT allows rollback to specific points, and LOCK TABLE controls concurrency by securing table access. These features ensure transactional integrity and data consistency in concurrent environments .

Values can be assigned to PL/SQL variables through direct assignments using the := operator or by executing a SELECT statement with INTO clause. In direct assignments, the value is directly calculated from expressions or other variables, e.g., 'tax := price * tax_rate;' . The SELECT INTO allows for database values to be directly assigned to a variable, e.g., 'SELECT SAL INTO CUR_SAL FROM EMP WHERE EID = ‘E12345’;'. This method fetches data from the database, providing a mechanism to link database operations with PL/SQL procedural logic .

PL/SQL syntax rules dictate that all commands must terminate with a semicolon. Additionally, statements are coded in a free field format, meaning they do not need to be aligned in specific columns. The case of commands is irrelevant, meaning they can be written in uppercase or lowercase. Comments can be added using -- for single-line comments and /* */ for block comments .

PL/SQL supports several logical operations and predicates which include simple logic operations and logical operators like AND, OR, NOT, as well as predicates like BETWEEN, IS NULL, LIKE, EXISTS, ANY, and ALL. These operations and predicates enhance the ability to form complex conditional expressions in PL/SQL programs, thereby increasing their control flow capabilities .

Using the %type attribute is advantageous because it abstracts the datatype to always match the definition used in the database schema. This minimizes errors and maintenance efforts if the underlying schema changes. For instance, if a column's datatype changes in the database, variables using %type automatically adapt to this change without needing modification, ensuring consistency across code and database schema .

The %type attribute allows a variable to be declared with the same datatype as a column in a database table without needing to know the column's exact type. This provides flexibility and reduces maintenance burden, as changes to the column type do not require altering the variable declaration .

The %rowtype attribute is used to declare a record in PL/SQL that corresponds to the row structure of a database table or view. This is significant because it allows developers to work with complete rows as a single entity, ensuring that the record has the same types and structure as the SELECT command used for retrieval. This approach is advantageous for data integrity and reduces the complexity of handling individual columns explicitly when processing database rows .

Values can be assigned to PL/SQL variables using direct assignments with the := operator, which is a straightforward expression evaluation like ‘tax := price * tax_rate’ . Another method is through a SELECT statement, like ‘SELECT SAL INTO CUR_SAL FROM EMP WHERE EID = ‘E12345’’, which retrieves a database value and assigns it to a variable . The main difference is that direct assignment uses expressions and existing variable values, while SELECT-based assignments obtain data directly from the database.

A constant in PL/SQL is defined similarly to a variable but includes the keyword CONSTANT and must be assigned an initial value at the time of declaration, which cannot be changed later. For example, a constant is declared as 'bonus_multiplier CONSTANT NUMBER (3,2) := 0.10;', indicating that the value of 'bonus_multiplier' cannot be altered after its declaration .

You might also like