Page 1 of 1

What is a Procedure in SQL? And how to create a Procedure in SQL?

Posted: Sun Feb 02, 2025 6:00 am
by soniya55531
Learn what a procedure is in SQL! See how to create procedures in MySQL and how to use them with practical examples.

If you prefer this content in video lesson format, watch the video below or access our YouTube channel !


SQL Icon
SQL
Impressive
Master the most important language in the world for those who work with Data, whether to stand out in your current company, to gain an edge in selection processes or even to enter data areas.

Start now
Right arrow
SQL icon used as backgroundThree images of SQL course classes

What is a Procedure in SQL?
In today's class, I'll show you what a procedure is in SQL. I'll guide you step by step through creating a procedure and how to use it in practical examples.

You will understand the structure of the procedure to understand its syntax within MySQL.

Next, we will explore two practical examples using SQL procedures to insert and delete data from a table. This way, you will learn how to apply the procedure in a real context.

So, do you already know how to use cryptocurrency data SQL procedures? Not yet? Then come with me and I'll teach you!

What is a Procedure in SQL
Procedures in SQL are blocks of code that can be stored and executed in the database.

In SQL, we use several commands to manipulate and interact with databases, and a procedure allows us to gather and group these commands into a block of code that can be called for execution whenever we want.

When creating a procedure, we need to define a name for it, set custom delimiters , and pass the necessary parameters that it will use when called. The basic syntax of the procedure is as follows:

Whenever we work with many lines of code in SQL, these lines need to be delimited using a separator, which by default is the semicolon ( ; ).

This indicates to SQL that that code, that command, must be executed up to that step.

As in procedures we want to work with a block of code, with several commands being executed at once, we need to adjust the delimiter so that SQL does not understand that with each semicolon, the command has finished.

So we change the delimiter to another character temporarily. So before creating a procedure, let's first change the delimiter by defining a new character or set of characters.

You can define any characters you want as a temporary delimiter, but it is important that the characters you choose do not interfere with existing SQL syntax. Therefore, it is very common to see the use of $$ as a temporary delimiter.

After setting the new delimiter, SQL will execute the entire block of code contained within it, until the end of that delimiter. After ending the temporary delimiter, we can revert back to the default delimiter .

Procedure Creation
A procedure is created using the create procedure command followed by the name of the procedure and, in parentheses, the parameters required for it to function correctly.

The same procedure can have several parameters of different types , such as integers, varchar, and so on.

After including and defining the parameters and their types, we continue with the begin block , where the code is effectively developed.

Let's look at two practical examples of creating and using procedures in SQL so that you can understand the entire process of creating and applying a procedure in your code.