Introduction to SQL Stored Procedures

Ali Mohammad
4 min readApr 28, 2023

--

This article offers an overview of Stored procedures.

🧐 What is Stored Procedures

Simply put, stored procedures are collections of one or more SQL statements that are kept for future use. Although you can think about them as batch files, they are more than that.

SQLite does not support stored procedures.

🔗 Understanding Why to Use Stored Procedures

  1. To simplify complex operations by encapsulating them into a single, simple-to-use unit.
  2. To assure data consistency without requiring repeated creation of a series of steps. If all developers and applications utilize the same stored procedure, everyone will use the same code.
  3. To simplify the management of change. If tables, column names, business logic, or anything else changes, just the stored procedure code must be modified, and no one else needs to be made aware of the changes.
  4. To reduce the amount of effort required to process the command. The DBMS performs less work because stored procedures are often stored in compiled form. This leads to enhanced performance.
  5. To create code that is more robust and adaptable. There are SQL language features and parts that are only accessible within a single request. This is why stored procedures can use them.

The syntax of stored procedures varies drastically across DBMSs. In reality, it is exceptionally difficult to create fully portable stored procedures.

💎 Creating Stored Procedures

CREATE OR REPLACE PROCEDURE exists in PostgreSQL to create a new procedure or replace an existing definition with a new one. For a PostgreSQL user to be able to CREATE OR REPLACE PROCEDURE, the USAGE privilegeon the language is required.

When a schema name is specified, a procedure is generated in schema. Otherwise, the current schema contains the newly constructed one. Plus, the CREATE or REPLACE PROCEDURE can be used to modify or replace the current procedure’s definition.

Now, not leave SQL Server out of the picture, here’s a couple of examples using it

This stored procedure takes no parameters at all. The calling application retrieves the value by using SQL Server’s return code support. Here a local variable named @news_count is declared using the DECLARE statement. This variable is then used in the SELECT statement so that it contains the value returned by the COUNT() function. Finally, the RETURN statement is used to return the count to the calling application as RETURN @news_count .

Executing Stored Procedures

This code declares a variable to hold whatever the stored procedure returns, executes the stored procedure, and then uses a SELECT to display the returned value.

🤓 Stored Procedures and Functions

In contrast to a function, a stored procedure does not return a value. The explanation is straightforward: the sole function of a stored procedure is to accomplish an action.

Let’s create a Stored Procedure

Let’s call it

Let’s create a Function now

Let’s call it

🆚 The following distinguishes stored procedures from functions:

  • When utilizing INOUT parameters, stored procedures are not required to return anything and only return a single row.
  • In stored procedures, you can commit and rollback transactions, but not in functions.
  • You execute a stored procedure using the CALL/EXECUTE statement rather than a SELECT statement.
  • Procedures cannot be layered within other DML commands, unlike functions.

😎 Advantages..

  • As frequently as feasible, stored Procedures can be reintroduced, which drastically reduces the number of trips between application and database servers.
  • In lieu of sending SQL statements to retrieve results and eliminate response time, the application must now merely invoke a function.
  • The application’s efficiency is enhanced. The PostgreSQL database server has pre-combined the user-defined functions and stored procedures.

I hope you found this article interesting and fun 💕!

--

--

Ali Mohammad
Ali Mohammad

Written by Ali Mohammad

A simple developer who loves to write code!

No responses yet