Plsql Journal Introduction 2024
Plsql Journal Introduction 2024
1. PL/SQL block: This is the component which has the actual PL/SQL code.This
consists of different sections to divide the code logically (declarative section
for declaring purpose, execution section for processing statements, exception
handling section for handling errors). It also contains the SQL instruction that
used to interact with the database server.
All the PL/SQL units are treated as PL/SQL blocks, and this is the starting stage of the
architecture which serves as the primary input.
Following are the different type of PL/SQL units.
Anonymous Block and Named Block--Function, Library, Procedure, Package Body,
Package Specification, Trigger, Type, Type Body
2. PL/SQL Engine:
PL/SQL engine is the component where the actual processing of the codes takes
place.
PL/SQL engine separates PL/SQL units and SQL part in the input The separated
PL/SQL units will be handled by the PL/SQL engine itself. The SQL part will be
sent to database server where the actual interaction with database takes place.
It can be installed in both database server and in the application server.
3. Database Server:
This is the most important component of PL/SQL unit which stores the data.
The PL/SQL engine uses the SQL from PL/SQL units to interact with the database
server.
It consists of SQL executor which parses the input SQL statements and execute the
same.
Structure of PL/SQL Block:
The basic unit in PL/SQL is a block. All PL/SQL programs are made up of blocks,
which can be nested within each other. Blocks contain both PL/SQL as well as SQL
instruction. All these instruction will be executed as a whole rather than executing
a single instruction at a time.
. Below are different sections of PL/SQL blocks.
Typically, each block performs a logical action in the program. A block has the
following structure:
DECLARE
declaration statements;
BEGIN
executable statements
EXCEPTIONS
exception handling statements
END;
Declaration Section :This is the first section of the PL/SQL blocks. This section is an
optional part. This is the section in which the declaration of variables, cursors,
exceptions, subprograms, that are needed in the block will be
declared.Declaration section starts with DECLARE keyword
Execution Section: Execution part is the main and mandatory part which actually
executes the code that is written inside it. Since the PL/SQL expects the executable
statements from this block this cannot be an empty block, i.e., it should have at
least one valid executable code line in it. Below are few more characteristics of
this part.
This can contain both PL/SQL code and SQL code.
This can contain one or many blocks inside it as a nested block.
This section starts with the keyword 'BEGIN'.
This section should be followed either by 'END' or Exception-Handling
section (if present) The Keyword 'END' marks the end of PL/SQL block.
Types of PL/SQL block: PL/SQL blocks are of mainly two types.
PL/SQL, the procedural language extension for SQL, provides several block
structures that allow you to organize and control the flow of your code. These block
structures include the following:
1. Anonymous Blocks:
o Anonymous blocks are sections of code that do not have a name and are
executed directly.
o They are typically used for ad-hoc or one-time operations.
o Anonymous blocks begin with the keyword BEGIN and end with the keyword
END;.
o Here's an example of an anonymous block that displays a message:
synatax:-
DECLARE
-- variable
declarations BEGIN
-- executable
statements END;
2. Named Blocks: These are named PL/SQL blocks that can be stored in the
database and executed later. Named blocks are typically used for reusable code
or stored procedures. They follow the same structure as anonymous blocks but
have a name that can be referenced for execution.
These blocks can be called from other blocks.
The block structure is same as an anonymous block, except it will never start with
the keyword 'DECLARE'. Instead, it will start with the keyword 'CREATE' which
instruct the compiler to create it as a database object.
These blocks can be nested within other blocks. It can also contain nested blocks
Example:
CREATE OR REPLACE PROCEDURE
my_procedure AS BEGIN
-- executable
statements END
my_procedure;
1. Stored Procedures
A procedure (often called a stored procedure) is a collection of
pre-compiled SQL statements stored inside the database. It is a
subroutine or a subprogram in the regular computing language. A
procedure always contains a name, parameter lists, and SQL
statements.
Stored Procedure Features
o Stored Procedure increases the performance of the applications.
o Stored procedure reduces the traffic between application and database server..
o Stored procedures are reusable and transparent to any applications.
o A procedure is always secure. The database administrator can grant
permissions to applications that access stored procedures in the database
without giving any permissions on the database tables.
How to create a procedure?
The following syntax is used for creating a stored procedure in MySQL. It can return
one or more value through parameters or sometimes may not return at all. By
default, a procedure is associated with our current database. But we can also create
it into another database from the current database by specifying the name as
database_name.procedure_name.
syntax:
1. DELIMITER &&
2. CREATE PROCEDURE procedure_name [[IN | OUT | INOUT] parameter_name datatype [, parameter
datatype]) ]
3. BEGIN
4. Declaration_section
5. Executable_section
6. END &&
7. DELIMITER ;
Parameter Explanations
The procedure syntax has the following parameters:
mysql> delimiter ;
mysql> call
getemp(101);
+-------+-------+------+--------+--------+------------
| empid | ename | age | salary | deptno |
+-------+-------+------+--------+--------+------------
| 101 | varun | 23 | 10000 | 1 |
+-------+-------+------+--------+--------+-------------
1 row in set (0.00 sec)
INOUT parameters
It is a combination of IN and OUT parameters. It means the calling program can pass the argument,
and the procedure can modify the INOUT parameter, and then passes the new value back to the
calling program.
In this example, the stored procedure SetCounter() accepts one INOUT parameter ( counter )
and one IN parameter ( inc ). It increases the counter ( counter ) by the value specified by the
inc parameter.
These statements illustrate how to call the Setcounter stored procedure:
SET @counter = 1;
CALL
SetCounter(@counter,1); --
2 CALL
SetCounter(@counter,1); --
3 CALL
SetCounter(@counter,5); --
8 SELECT @counter; -- 8
2.Functions
Functions are similar to stored procedures but return a value. They can be used in SQL
statements or other PL/SQL code to perform calculations or return specific values.
Functions can have input parameters and a return type.
Syntax
CREATE OR REPLACE FUNCTION function_name (parameter1 datatype,
parameter2 datatype) RETURN return_type
IS
-- local variable
declarations BEGIN
-- code statements
here RETURN
return_value; END;
Parameter:
Function_name: name of the function
Parameter: number of parameter. It can be one or
more than one. return_datatype: return value
datatype of the function declaration_section: all
variables are declared. executable_section: code for
the function is written here
A deterministic : function always returns the same result for the same input parameters, while a non-deterministic
function produces different results for the same input parameters.
Drop a function
In MySQL Function can also be dropped.
Syntax:
1. Drop function [ IF EXISTS ] function_name;
Parameter
function_name: name of the function to be dropped.
Here's an example:
DECLARE EXIT HANDLER FOR
SQLEXCEPTION BEGIN
-- Handle the exception here
SELECT 'An error occurred' AS
error_message; END;
3. NOT FOUND: The NOT FOUND exception is raised when a SELECT INTO
statement does not find any rows to assign to variables. It can be used to
handle situations where a query does not return any results.
Here's an example:
Here's an example:
DECLARE EXIT HANDLER FOR
DUP_VAL_ON_INDEX BEGIN
-- Handle the exception here
SELECT 'Duplicate value on index' AS
error_message; END;
5. TOO_MANY_ROWS: The TOO_MANY_ROWS exception is raised when a SELECT
INTO statement returns more than one row. It can be used to handle situations
where a query unexpectedly returns multiple rows.
Here's an example:
DECLARE EXIT HANDLER FOR
TOO_MANY_ROWS BEGIN
-- Handle the exception here
SELECT 'Too many rows found' AS
error_message; END;
4. A cursor in MySQL
In MySQL, a cursor is a database object that allows you to retrieve and manipulate data from a result set.
It provides a way to iterate over the rows returned by a SELECT statement, enabling you to perform
operations on each row individually.
Cursors are particularly useful when you need to perform complex operations on individual rows or
when you want to process a large result set in a controlled manner.
Deallocate the cursor: After closing the cursor, you should deallocate it to free up
the memory used by the cursor. Deallocating the cursor is done using the
DEALLOCATE statement. For example:
DEALLOCATE cursor_name;
Implicit cursors
In MySQL, a cursor is a database object that allows you to retrieve and manipulate
data from a result set. There are two types of cursors: explicit and implicit. In this
answer, we will focus on implicit cursors.
An implicit cursor is automatically created by MySQL when you execute a SELECT
statement. It is used to retrieve the result set generated by the SELECT statement.
Unlike explicit cursors, you do not need to explicitly declare or manage an implicit
cursor.
*************************** ******** ********************************