Updated March 18, 2023
Introduction to Procedure in SQL
The following article provides an outline for What is Procedure in SQL? Procedures or Stored Procedures in SQL are logical units of SQL queries that can be created once, fetched, and executed multiple times whenever there is a need to repeat the same operation. It also allows passing parameters, like the stored procedure with arguments in the OOPS concept. It is categorized under DML (Data Manipulation Language) of SQL programming, as it is applicable to operations involving data management and manipulation.
What is SQL?
Structured Query Language (SQL) is sometimes pronounced as the individual character “S-QL” or “see-Quel”. It is used to query, manipulated, or update data in the relational database. It is used to insert or update or delete records in a database. One of the main functionality of SQL is maintaining a database.
Syntax of SQL query to fetch all the records is as follows:
select * from table_name
Stored Procedures in SQL
What is a Stored Procedure? A Stored Procedure is a collection of Structured Query Language (SQL) statements with a name assigned to them. These stored procedures are stored in a relational database management system (RDBMS). So it is possible for multiple calling of the procedures to reduce multiple executions and resulting in reduced execution time. Data can be modified using procedures, and the major advantage is that it is not tied to a particular application.
Stored procedures can accept parameters as input and can return multiple values as an output parameter. In SQL Server, stored procedures perform operations in the database and can return a status value to an application or a batch calling procedure. User-defined procedures are created using the user-defined database, with the exception when we have a read-only database. For that, we need to develop it in Transact SQL (T-SQL). Tempdb is where all the temporary procedures are stored.
There are two types of stored procedures.
- Local procedures
- Global procedures
Local procedures are the one which is only visible for the user using it in the connection, whereas Global procedures are visible to any of the users in the same connection.
Structure of Creating the Procedure
Here, the owner means the database name, and to pass the parameter, we need to add the ‘@’ symbol.
Code:
CREATE PROCEDURE <owner>.<procedure name>
<param> <datatype>
AS
<Body>
Execute a Stored Procedure
Exec is the command for the execution of the procedure.
EXEC Procedure_name
Comparison Between Stored Procedure and Functions
- Stored procedures and functions can be used to achieve the same functionality.
- We can customize both procedures as well as functions.
- The difference between them is functions are designed to send their output to Transact SQL, and stored procedures are designed to return output to a query, as well as they can return output to T-SQL also.
- Applications can have input from the stored procedures, while user-defined functions can return table variables, and they are not privileged to change the environment settings neither operating system environment.
Database Company
Given below are the database company:
Company | Category | Item |
Tarle | edibles | cookies |
Tarle | edibles | chips |
Lakme | cosmetics | lipstick |
Brisket | grocery | Flax seeds |
Brisket | edibles | biscuits |
Brisket | grocery | oil |
ole | cosmetics | Nail polish |
ole | cosmetics | Hairbrush |
Stored Procedure Example
The following SQL statement creates a stored procedure that selects the company.
Code:
CREATE PROCEDURE SelectAllExample
AS
SELECT * FROM company GO;
Execute the stored procedure above as follows:
Executing the above-stored procedure, we can use the following command:
Code:
EXEC SelectAllExample;
Stored Procedure Example With One Parameter
The following SQL statement creates a stored procedure with a single parameter to be passed that selects a company with an item as a parameter.
Code:
CREATE PROCEDURE SelectAllExample @item nvarchar(30)
AS
SELECT * FROM company WHERE item = @item GO;
Execute the stored procedure above as follows:
To execute the above-stored procedure with one parameter, we can use the following command:
Code:
EXEC SelectAllExample item = "cookies";
Stored Procedure Example with Multiple Parameters
The following SQL statement creates a stored procedure with multiple parameters that select company with item and category as a parameter
Code:
CREATE PROCEDURE SelectAllExample @item nvarchar(30), @ category nvarchar(10)
AS SELECT * FROM company WHERE item = @ item AND category = @ category GO;
Execute the stored procedure above as follows:
To execute the above-stored procedure with multiple parameters, we can use the following command we need to pass the multiple parameters with comma-separated: EXEC SelectAllExample item =”cookies”, category =”edibles”;
Advantages and Disadvantages of Procedure in SQL
Below are the advantages and disadvantages mentioned:
Advantages:
- Application improvement can be achieved by procedures. If a procedure is called multiple times in a single application, then the compiled version of the procedure is utilized.
- Traffic between the database and the application can be reduced as the big statements are already fed into the database, and we don’t have to send it again and again.
- Using procedures, code reusability can be achieved, similar to the functions and methods that work in the java and other programming languages.
Disadvantages:
- Stored Procedures consume a lot of memory. It is the duty of a database administrator has to decide an upper bound to how many stored procedures are feasible for a particular application.
- Stored procedures cannot be debugged using MySQL.
Conclusion – What is Procedure in SQL
Simple non-select statements, including DML, statements like Insert and delete, also DDL statements such as Drop and Create can be included in the stored programs. Memory consumption is more, but the seek rate can be reduced, and code reusability is beneficial. Stored procedures can be implemented with or without variables and can be executed by passing the parameters if required.
Recommended Articles
We hope that this EDUCBA information on “What is Procedure in SQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.