SQL - Stored Procedures
SQL - Stored Procedures
It can be used to perform a wide range of database operations such as inserting, updating,
or deleting data, generating reports, and performing complex calculations. Stored
procedures are very useful because they allow you to encapsulate (bundle) a set of SQL
statements as a single unit and execute them repeatedly with different parameters,
making it easy to manage and reuse the code.
Syntax
The basic syntax to create an SQL stored procedure is as follows −
DELIMITER //
CREATE PROCEDURE procedure_name(parameter1 datatype, parameter2 datatype, ...)
BEGIN
-- SQL statements to be executed
END
DELIMITER ;
https://www.tutorialspoint.com/sql/sql-stored-procedures.htm 1/10
8/27/24, 4:09 PM SQL - Stored Procedures
The CREATE PROCEDURE statement is used to create the procedure. We can define
any number of input parameters as per the requirement.
The SQL statements that make up the procedure are placed between the BEGIN
and END keywords.
Ad
Creating a Procedure
We can create a stored procedure using the CREATE PROCEDURE statement in SQL.
Following are the simple steps for creating a stored procedure −
We can then test the stored procedure by executing it with different input
parameters.
Example
To understand it better let us consider the CUSTOMERS table which contains the personal
details of customers including their name, age, address and salary etc. as shown below −
https://www.tutorialspoint.com/sql/sql-stored-procedures.htm 2/10
8/27/24, 4:09 PM SQL - Stored Procedures
Now, insert values into this table using the INSERT statement as follows −
Now, let us look at a simple example of creating a stored procedure that takes an input
parameter and returns a result set.
In the following query, we are creating the stored procedure with the name
GetCustomerInfo. then we provide it with a single input parameter called @CutomerAge.
The stored procedure then selects all records from the CUSTOMERS table where the value
of the CutomerAge matches the input parameter.
DELIMITER //
CREATE PROCEDURE GetCustomerInfo(IN CustomerAge INT)
BEGIN
SELECT * FROM CUSTOMERS WHERE AGE = CustomerAge;
https://www.tutorialspoint.com/sql/sql-stored-procedures.htm 3/10
8/27/24, 4:09 PM SQL - Stored Procedures
END //
DELIMITER ;
Output
This would produce the following result −
Verification
We can test the stored procedure by executing it using the CALL statement as shown
below −
CALL GetCustomerInfo(25);
This will return all columns from the CUSTOMERS table where the customers age is 25.
Learn SQL in-depth with real-world projects through our SQL certification course. Enroll
and become a certified expert to boost your career.
Ad
https://www.tutorialspoint.com/sql/sql-stored-procedures.htm 4/10
8/27/24, 4:09 PM SQL - Stored Procedures
Stored procedures in a database system can have different types of parameters, which are
placeholders for values that will be passed to the stored procedure when it is executed.
Following are the different types of stored procedure parameters in SQL −
Input parameters
1 These parameters are used to pass values from the calling statement to the
stored procedure.
Output parameters
2
These parameters are used to return values from the stored procedure.
Input/Output parameters
3 These parameters allow a stored procedure to accept input values and return
output values.
These values are read-only, so they cannot be modified by the stored procedure.
Example
In the following query, we are creating a stored procedure that takes a customer ID as an
input parameter and returns the corresponding customer salary.
The procedure body simply performs a SELECT statement to retrieve the "Salary" column
from the "CUSTOMERS" table, where the "CustomerID" matches the input parameter.
DELIMITER //
CREATE PROCEDURE GetCustomerSalary(IN CustomerID Int)
BEGIN
SELECT SALARY FROM CUSTOMERS WHERE ID = CustomerID;
END //
DELIMITER ;
Output
This would produce the following result −
https://www.tutorialspoint.com/sql/sql-stored-procedures.htm 5/10
8/27/24, 4:09 PM SQL - Stored Procedures
Verification
We can test it by executing it with different ID as an input parameter as shown in the
query below −
CALL GetCustomerSalary(6);
This will return the salary for the customer with an ID of 6, assuming there is a
corresponding row in the CUSTOMERS table −
SALARY
4500.00
Note that when using an OUT parameter, we must specify the keyword OUT before the
parameter name when passing it to the stored procedure. This tells the SQL database that
the parameter is an output parameter and should be assigned with a value in the stored
procedure.
Example
In the following query we are creating a stored procedure that used to count the number
of records of customer having same age and assign this count to the 'total' variable which
holds the number of records.
https://www.tutorialspoint.com/sql/sql-stored-procedures.htm 6/10
8/27/24, 4:09 PM SQL - Stored Procedures
The procedure body performs a SELECT statement to get the count of records having
same age from the "CUSTOMERS" table
DELIMITER //
CREATE PROCEDURE GetDetail(OUT total INT)
BEGIN
SELECT COUNT(AGE) INTO total FROM CUSTOMERS
WHERE AGE = 25;
END //
DELIMITER ;
CALL GetDetail(@total);
Here, we are using the SELECT statement and getting the count −
SELECT @total;
Output
This would produce the following result −
@total
Verification
To verify weather the procedure is created, we can use the following query −
https://www.tutorialspoint.com/sql/sql-stored-procedures.htm 7/10
8/27/24, 4:09 PM SQL - Stored Procedures
Example
In the following query, we provide two INOUT parameters to the stored procedure:
cust_id and curr_Salary. These two are used as both an input and output parameters.
The stored procedure first retrieves the current salary of the customer from the database
using the cust_id parameter. It then increases the salary by 10% and updates the
customers salary in the database using the same parameter.
DELIMITER //
CREATE PROCEDURE increaseSalary(INOUT Cust_Id Int, INOUT curr_Salary Int)
BEGIN
SELECT SALARY INTO curr_Salary From CUSTOMERS Where ID = Cust_Id;
SET curr_Salary = curr_Salary * 1.1;
Update CUSTOMERS SET SALARY = curr_Salary Where ID = Cust_Id;
END //
DELIMITER ;
Output
This would produce the following result −
Verification
We can test it by executing it with different ID or input parameters as shown in the query
below −
SET @customerID = 1;
SET @salary = 0.0;
CALL increaseSalary(@customerID, @salary);
Following is Query to select the updated salary from the stored procedure
updated_salary
2200
https://www.tutorialspoint.com/sql/sql-stored-procedures.htm 8/10
8/27/24, 4:09 PM SQL - Stored Procedures
Code Reuse: Stored procedures can be called from different client applications,
which means that the same code can be reused across different applications. This
reduces development time and maintenance costs.
Better Security: Stored procedures can be used to enforce security rules and
prevent unauthorized access to sensitive data. They can also limit the actions that
can be performed by users, making it easier to maintain data integrity and
consistency.
Security Risks: If stored procedures are not written correctly, they can pose a
security risk, particularly if they are used to access sensitive data or to perform
actions that could compromise the integrity of the database.
https://www.tutorialspoint.com/sql/sql-stored-procedures.htm 9/10
8/27/24, 4:09 PM SQL - Stored Procedures
https://www.tutorialspoint.com/sql/sql-stored-procedures.htm 10/10