The Wayback Machine - https://web.archive.org/web/20240930183752/https://www.geeksforgeeks.org/sql-procedures/
Open In App

Procedures in PL/SQL

Last Updated : 08 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

PL/SQL procedures are reusable code blocks that perform specific actions or logic within a database environment. They consist of two main components such as the procedure header which defines the procedure name and optional parameters and the procedure body which contains the executable statements implementing the desired business logic.

In this article, We will learn about PL/SQL Procedures in detail by understanding various examples and so on.

Procedures in PL/SQL

A PL/SQL procedure is a reusable block of code that contains a specific set of actions or logic.

The procedure contains two parts:

1. Procedure Header

  • The procedure header includes the procedure name and optional parameter list.
  • It is the first part of the procedure and specifies the name and parameters

2. Procedure Body

  • The procedure body contains the executable statements that implement the specific business logic.
  • It can include declarative statements, executable statements, and exception-handling statements

Create Procedures in PL/SQL

To create a procedure in PL/SQL, use the CREATE PROCEDURE command:

Syntax

CREATE PROCEDURE syntax is:

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE PROCEDURE procedure_name
@Parameter1 INT,
@Parameter2 VARCHAR(50) = NULL,
@ReturnValue INT OUTPUT
AS
BEGIN
END
GO

Note: Procedures in PL/SQL without parameters are written without parentheses after the procedure name

Example

In this example, we will create a procedure in PL/SQL

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE GetStudentDetails
@StudentID int = 0
AS
BEGIN
SET NOCOUNT ON;
SELECT FirstName, LastName, BirthDate, City, Country
FROM Students WHERE StudentID=@StudentID
END
GO

Parameters in Procedures

In PL/SQL, parameters are used to pass values into procedures. There are three types of parameters used in procedures:

1. IN parameters

  • Used to pass values into the procedure
  • Read-only inside the procedure
  • Can be a variable, literal value, or expression in the calling statement.

2. OUT parameters

  • Used to return values from the procedure to the calling program
  • Read-write inside the procedure
  • Must be a variable in the calling statement to hold the returned value

3. IN OUT parameters

  • Used for both passing values into and returning values from the procedure
  • Read-write inside the procedure
  • Must be a variable in the calling statement

Modify Procedures in PL/SQL

To modify an existing procedures in PL/SQL use the ALTER PROCEDURE command:

Syntax

ALTER PROCEDURE Syntax is:

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE procedure_name
@Parameter1 INT,
@Parameter2 VARCHAR(50) = NULL,
@ReturnValue INT OUTPUT

AS

BEGIN

— Query

END
GO

Example

In this example, we will modify a procedure in PL/SQL

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE GetStudentDetails
@StudentID int = 0
AS
BEGIN
SET NOCOUNT ON;
SELECT FirstName, LastName, City
FROM Students WHERE StudentID=@StudentID
END
GO

Drop Procedure in PL/SQL

To drop a procedure in PL/SQL use the DROP PROCEDURE command

Syntax

DROP PROCEDURE syntax is:

DROP PROCEDURE procedure_name

PL/SQL DROP PROCEDURE Example

In this example, we will delete a procedure in PL/SQL

DROP PROCEDURE GetStudentDetails

Advantages of Procedures

  • They result in performance improvement of the application. If a procedure is being called frequently in an application in a single connection, then the compiled version of the procedure is delivered.
  • They reduce the traffic between the database and the application since the lengthy statements are already fed into the database and need not be sent again and again via the application.
  • They add to code reusability, similar to how functions and methods work in other languages such as C/C++ and Java.

Disadvantages of Procedures

  • Stored procedures can cause a lot of memory usage. The database administrator should decide an upper bound as to how many stored procedures are feasible for a particular application.
  • MySQL does not provide the functionality of debugging the stored procedures.

Important Points About Procedures in SQL

  • A procedure in PL/SQL is a subprogram that can take parameters and be called to perform a specific action.
  • Procedures are executed just like SQL statements.
  • Procedures have two parts the specification (spec) and the body. The spec begins with the PROCEDURE keyword and ends with the procedure name and optional parameter list. The body begins with IS (or AS) and ends with END followed by an optional procedure name
  • They enhance performance by reducing network traffic between the application and database.

Conclusion

PL/SQL procedures are essential for efficient database management, providing a way to encapsulate and execute business logic. They help in enhancing performance by minimizing network traffic and improving execution efficiency. Despite their advantages, procedures can increase memory usage and lack debugging functionality in some databases.

FAQs

What are the main components of a PL/SQL procedure?

A PL/SQL procedure consists of two main parts: the procedure header, which includes the procedure name and optional parameters, and the procedure body, which contains the executable statements that define the procedure’s logic.

How do IN, OUT, and IN OUT parameters differ in PL/SQL procedures?

  • IN Parameters: Pass values into the procedure and are read-only within the procedure.
  • OUT Parameters: Return values from the procedure to the calling program and are read-write within the procedure.
  • IN OUT Parameters: Allow both passing values into and returning values from the procedure and are read-write within the procedure.

How can you modify an existing PL/SQL procedure?

To modify an existing procedure, use the ALTER PROCEDURE command followed by the procedure name and updated definition. This command allows you to change the procedure’s parameters and logic without dropping it entirely.



Similar Reads

Advantages and Disadvantages of Using Stored Procedures - SQL
A Stored Procedure is a type of code in SQL that can be stored for later use and can be used many times. So, whenever you need to execute the query, instead of calling it you can just call the stored procedure. You can also pass parameters to a stored procedure, so that the stored procedure can act based on the parameter values that is passed. Synt
3 min read
Some Useful System Stored Procedures in SQL
Some Useful System Stored Procedures in SQL. These procedures are built-in procedures and will help to extract the definition and dependencies of the existing user-defined stored procedures. sp_help : This will display the Stored procedure Name, Schema Name, created date, and Time or if there are any parameters, then Parameter Name, Data Type, Leng
2 min read
Stored Procedures classification based on Input and Output Parameters in SQL Server
Stored Procedure: The stored procedure has the following key points as follows. It is a collection of SQL statements such as if you want to write and read data from a database then you can use create a statement for write and select statement for reading the data from database and SQL command logic for the same, which is compiled and stored on the
2 min read
How to Boost Speed of Queries in Stored Procedures in PostgreSQL?
Usually, when we have large procedures in PostgreSQL we find them taking a lot of time (for eg. a task that would take normally 5 mins would go for more than 30 mins). The Major factor here which causes is that when we have more updates and insert statements in our procedure and the amount of data inserted or updated is huge then Postgres is not ab
3 min read
SQL VS ADO.NET With Stored Procedures
Stored Procedures have been used as a solution for so many problems, performance issues and not being able to separate the business Logic and transaction management. Stored Procedure tries to solve this major problem while developing a website. In .NET when we want to create a website or desktop application and when we have to query the database we
6 min read
How to List all Stored Procedures in MariaDB?
When working with MariaDB, it's important to be able to manage and maintain stored procedures effectively. Listing all stored procedures in a database can provide valuable insights into its structure and functionality. In this article, we'll explore how to list all stored procedures in MariaDB by understanding the various approaches along with the
4 min read
What are Stand-Alone Procedures?
Stand-Alone procedures are fundamental components in modern database systems. They make­ things organized, fast, and safe. Learning about the­se procedures he­lps people managing databases to cre­ate good ones. Using simple proce­dures right means data work flows smoothly. In this article, we will see about stand-alone procedures. It will cove­r wh
5 min read
SQL Stored Procedures
A stored procedure in SQL is a group of SQL queries that can be saved and reused multiple times. It is very useful as it reduces the need for rewriting SQL queries. It enhances efficiency, reusability, and security in database management. Users can also pass parameters to stored procedures so that the stored procedure can act on the passed paramete
4 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced
SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. Whether we are beginners or experienced professionals, practicing SQL exercises is important for improving your skills. Regular practice helps you get better at using SQL and boosts your confidence in handling different database tasks. So, in
15+ min read
SQL Cheat Sheet ( Basic to Advanced)
Creating and managing databases in SQL involves various commands and concepts that handle the structuring, querying, and manipulation of data. In this guide, we will see a comprehensive cheat sheet for essential SQL operations, offering a practical reference for tasks ranging from database creation to advanced data handling techniques. It includes
15 min read
MySQL Tutorial
This MySQL Tutorial is made for both beginners and experienced professionals. Whether you're starting with MYSQL basics or diving into advanced concepts, this free tutorial is the ideal guide to help you learn and understand MYSQL, no matter your skill level. From setting up your database to performing complex queries and database administration ta
11 min read
SQL Tutorial
In this SQL tutorial, you’ll learn all the basic to advanced SQL concepts like SQL queries, SQL join, SQL injection, SQL insert, and creating tables in SQL. SQL is easy to learn, there are no prerequisites to learn SQL. So, SQL is a standard database language used to access and manipulate data in databases. SQL stands for Structured Query Language.
11 min read
SQL Query Interview Questions
SQL or Structured Query Language is a standard language for relational databases. SQL queries are powerful tools used to, manipulate, and manage data stored in these databases like MySQL, Oracle, PostgreSQL, etc. Whether you're fetching specific data points, performing complex analyses, or modifying database structures, SQL queries provide a standa
10 min read
MySQL Interview Questions
MySQL is a Free open-source Relational Database Management System(RDMS) that stores data in a structured tabular format using rows and columns. It uses Structured Query Language (SQL) for accessing, managing, and manipulating databases. It was originally developed by MySQL AB, a Swedish company, and is now owned by Oracle Corporation. It's known fo
13 min read
Window functions in SQL
Window functions apply to aggregate and ranking functions over a particular window (set of rows). OVER clause is used with window functions to define that window. OVER clause does two things : Partitions rows to form a set of rows. (PARTITION BY clause is used) Orders rows within those partitions into a particular order. (ORDER BY clause is used) N
4 min read
SQL Trigger | Student Database
A trigger is a stored procedure in a database that automatically invokes whenever a special event in the database occurs. For example, a trigger can be invoked when a row is inserted into a specified table or when specific table columns are updated. In simple words, a trigger is a collection of SQL statements with particular names that are stored i
7 min read
CTE in SQL
The Common Table Expressions (CTE) were introduced into standard SQL in order to simplify various classes of SQL Queries for which a derived table was just unsuitable. CTE was introduced in SQL Server 2005, the common table expression (CTE) is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement.
6 min read
SQL Data Types
A column's data type is essentially the type of data format that will be used to store the data in each cell; examples include any type of integer, character, money, date and time, binary, etc. We'll acquire in-depth information about SQL Data Types in this tutorial. SQL Data TypesAn SQL developer must know what data type will be stored inside each
4 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands
SQL commands are extensively used to interact with databases, enabling users to perform a wide range of actions on database systems. Understanding these commands is crucial for effectively managing and manipulating data. This guide will introduce you to the various SQL sublanguage commands, including Data Definition Language (DDL), Data Query Langu
6 min read
SQL | Subquery
In SQL a Subquery can be simply defined as a query within another query. In other words we can say that a Subquery is a query that is embedded in WHERE clause of another SQL query. Important rules for Subqueries: You can place the Subquery in a number of SQL clauses: WHERE clause, HAVING clause, FROM clause. Subqueries can be used with SELECT, UPDA
3 min read
How to find Nth highest salary from a table?
Structured Query Language is a computer language that we use to interact with a relational database. Finding Nth highest salary in a table is the most common question asked in interviews. Here is a way to do this task using the dense_rank() function. Consider the following table: Employee: CREATE TABLE: CREATE TABLE emp ( emp_name VARCHAR(50), emp_
4 min read
ALTER (RENAME) in SQL
Sometimes we may want to rename our table to give it a more relevant name. For this purpose, we can use ALTER TABLE to rename the name of the table. SQL ALTER TABLE is a command used to modify the structure of an existing table in a database. Here we will discuss Alter Command in SQL in detail. Alter in SQLHere we are discussing the syntax of Alter
2 min read
SQL | WITH Clause
The SQL WITH clause, also known as Common Table Expressions (CTEs), was introduced by Oracle in the Oracle 9i release 2 database. The SQL WITH clause allows you to give a sub-query block a name (a process also called sub-query refactoring), which can be referenced in several places within the main SQL query.  What is the SQL WITH Clause?The clause
3 min read
SQL Interview Questions
SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM Computer Scientists in the 1970s. By executing queries, SQL can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that
15+ min read
SQL Views
Views in SQL are a kind of virtual table. A view also has rows and columns like tables, but a view doesn't store data on the disk like a table. View defines a customized query that retrieves data from one or more tables, and represents the data as if it was coming from a single source. We can create a view by selecting fields from one or more table
8 min read
SQL | GROUP BY
The GROUP BY Statement in SQL is used to arrange identical data into groups with the help of some functions. i.e. if a particular column has the same values in different rows then it will arrange these rows in a group.  FeaturesGROUP BY clause is used with the SELECT statement.In the query, the GROUP BY clause is placed after the WHERE clause.In th
4 min read
SQL CREATE TABLE
CREATE TABLE command creates a new table in the database in SQL. In this article, we will learn about CREATE TABLE in SQL with examples and syntax. SQL CREATE TABLE StatementSQL CREATE TABLE Statement is used to create a new table in a database. Users can define the table structure by specifying the column's name and data type in the CREATE TABLE c
3 min read
SQL Joins (Inner, Left, Right and Full Join)
SQL Join operation combines data or rows from two or more tables based on a common field between them. In this article, we will learn about Joins in SQL, covering JOIN types, syntax, and examples. SQL JOINSQL JOIN clause is used to query and access data from multiple tables by establishing logical relationships between them. It can access data from
5 min read
SQL SELECT Query
The SQL SELECT Statement retrieves data from a database. SELECT Statement in SQLThe SELECT statement in SQL is used to fetch or retrieve data from a database. It allows users to access the data and retrieve specific data based on specific conditions. We can fetch either the entire table or according to some specified rules. The data returned is sto
3 min read
SQL Query to Find Second Highest Salary
There are a lot of real-world scenarios where a query for finding the nth salary will be really useful. Considering finding the second-highest salary in SQL, we have one sample table. Finding Second Highest SalaryConsider below simple table: Name Salary Aman100000Shubham1000000Naveen 40000Nishant500000How to find the employee whose salary is the se
3 min read
Article Tags :