On this page
- 1 SQL: Stored Procedure
- 2 SQL: Stored Procedure
- 2.1 Syntax
- 2.2 Example
- 2.3 Calling a Stored Procedure
- 2.3.0.1 Insert data using a CSV file
- 2.3.0.2 Connecting to a MySQL Database and Creating a Backup
- 2.3.0.3 SQL: Indexing in Databases
- 2.3.0.4 SQL: Joins (Inner, Left, Right and Full Joins)
- 2.3.0.5 SQL Transactions: BEGIN, COMMIT, ROLLBACK
- 2.3.0.6 Managing Duplicate Rows in SQL
- 2.3.0.7 Structured Query Language (SQL)
- 2.4 Leave A Comment Cancel reply
SQL: Stored Procedure
SQL: Stored Procedure
A stored procedure is a collection of SQL statements that are stored in the database. A stored procedure can contain business logic, which is one of the key aspects that distinguish stored procedures from views. A stored procedure can accept parameters, and you can set variables, write IF statements, etc within a stored procedure.
A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again. So if you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it. You can also pass parameters to a stored procedure so that the stored procedure can act based on the parameter value(s) that is passed.
On this page
Syntax
CREATE PROCEDURE sp_name(p_1 INT) BEGIN ...code goes here... END;
Example
Calling a Stored Procedure
We can use CALL keyword to invoke stored procedures.
CALL spGetPosts()