Chapter 4_Using PLSQl Sub Program (1)
Chapter 4_Using PLSQl Sub Program (1)
PL/SQL Subprograms
• A program separate from the main program that executes a series of operations
that occurs multiple times during the machine cycle.
• A PL/SQL subprogram is a named PL/SQL block that can take parameters and be
invoked repeatedly. If the subprogram has parameters, their values can differ for
each invocation.
• Each subprogram will have a name, and also has parameter list same like that of
the anonymous PL/SQL blocks, these have three parts:
Advance Database using PL/SQL
Unit 4 : Using PL/SQL Subprograms
Each subprogram will have a name, and also has parameter list same
like that of the anonymous PL/SQL blocks, these have three parts:
• Declarative part – It is an optional part. However, the declarative part for a
subprogram does not start with the DECLARE keyword. It contains declarations
of types, cursors, constants, variables, exceptions, and nested subprograms.
These items are local to the subprogram and cease to exist when the
subprogram completes execution.
• Executable part – Executable part is said to be the mandatory part and
also has statements that will perform the designated actions.
• Exception-handling – Exception-handling is said to be an optional part
and also contains code that can handle run-time errors.
Advance Database using PL/SQL
Unit 4 : Using PL/SQL Subprograms
Advantages of Subprograms
In Microsoft SQL Server, subprograms are typically implemented using stored procedures, functions, and triggers.
These subprograms offer various advantages that help in managing and manipulating data within the database.
Here are some of the advantages of using subprograms in MS SQL Server:
• Code Reusability: Subprograms allow you to encapsulate a set of SQL statements into a single, reusable unit.
This reduces code duplication and promotes a more modular and maintainable approach to database
development.
• Improved Performance: SQL Server can optimize the execution plan for stored procedures and functions,
resulting in improved query performance.
• Security: You can grant specific permissions to stored procedures and functions, allowing you to control who can
access and modify the data in your database. This helps enhance data security.
• Encapsulation: Subprograms encapsulate business logic and data manipulation within the database. This
separation of concerns makes it easier to maintain and update your application, as changes are isolated to the
subprograms themselves.
• Error Handling: Stored procedures and functions can include error handling logic, making it easier to handle and
report errors in a consistent and controlled manner. This is particularly useful for handling exceptions and
recovering from errors.
Advance Database using PL/SQL
Unit 4 : Using PL/SQL Subprograms
Advantages of Subprograms
• Maintainability: Subprograms make it easier to maintain and update your database schema and
logic. If you need to change a specific operation, you only need to update the subprogram
instead of searching for and modifying multiple instances of the same code.
• Transaction Control: You can explicitly define transaction boundaries within stored procedures.
This allows you to ensure that a series of related SQL statements either all succeed or all fail,
which is crucial for maintaining data consistency.
• Modularity: Subprograms allow you to break down complex operations into smaller,
manageable pieces. This modular approach makes it easier to understand, test, and debug your
code.
• Centralized Logic: By using subprograms, you centralize your data access and manipulation
logic. This ensures that data-related operations are consistent across your application, reducing
the risk of data inconsistencies.
• Parameterization: Stored procedures and functions allow you to use parameters to pass values
to the code, making it more flexible and versatile for various use cases.
Advance Database using PL/SQL
Unit 4 : Using PL/SQL Subprograms
PL/SQL Procedures
• This is subprograms which do not return a value directly; mainly used to
perform an action.
PL/SQL Procedures
Creating a Procedure
A procedure is created with the CREATE OR REPLACE PROCEDURE statement.
The simplified syntax for the CREATE OR REPLACE PROCEDURE statement is as
follows −
CREATE PROCEDURE procedure_name
AS
sql_statement
GO;
EXEC GetAllProduct
Advance Database using PL/SQL
Unit 4 : Using PL/SQL Subprograms
===========================================
==
-- Author:<Suraj>
-- Create date: <10/30/2023>
-- Description:<For selecting all product>
===========================================
==
ALTER PROCEDURE GetAllProduct
AS
BEGIN
select ProductName,Salesprice from Product
END
Advance Database using PL/SQL
Unit 4 : Using PL/SQL Subprograms
PL/SQL Function
A function is a database object that allows you to encapsulate a specific piece of
Transact-SQL (T-SQL) logic and return a single value or a table of values as a result.
Functions in SQL Server help improve code organization, maintainability, and
reusability while allowing you to encapsulate specific logic for various data
manipulation tasks.
SQL Server provides two main types of user-defined functions:
• Scalar Function: A scalar function in SQL Server returns a single value, such as an
integer, string, date, or any other data type. It takes input parameters and performs
some calculations or operations on these parameters to produce a single result.
• Table-Valued Function: A table-valued function in SQL Server returns a table of
rows as its result. It can be used to return a set of rows based on some logic or
calculations, making it particularly useful for complex queries or when you need to
reuse a set of rows.
Advance Database using PL/SQL
Unit 4 : Using PL/SQL Subprograms
RETURN @result
END
SELECT dbo.CalculateFactorial(5) AS FactorialOf5
Advance Database using PL/SQL
Unit 4 : Using PL/SQL Subprograms
Does not allow the use of Try…Catch blocks for exception handling. Allows use of Try…Catch blocks for exception handling.
Cannot have transactions within a function. Can have transactions within a stored procedure.
Cannot call a stored procedure from a function. Can call a function from a stored procedure.
Temporary tables cannot be used within a function. Only table Both table variables and temporary tables can be used.
variables can be used.
Functions can be called from a Select statement. Stored procedures cannot be called from a Select/Where or Having
statements. Execute statement has to be used to execute a stored
procedure.
Functions can be used in JOIN clauses. Stored procedures cannot be used in JOIN clauses
Advance Database using PL/SQL
Unit 4 : Using PL/SQL Subprograms
WITH RecursiveFactorial AS (
-- Anchor query
SELECT 1 AS N, 1 AS Result
UNION ALL
-- Recursive query
SELECT N + 1, Result * (N + 1)
FROM RecursiveFactorial
WHERE N < 5 -- Termination condition
)
SELECT Result
FROM RecursiveFactorial
WHERE N = 5; -- Final result
Advance Database using PL/SQL
Unit 4 : Using PL/SQL Subprograms