SQL Keywords

SQL STORED PROCEDURES

What are SQL stored procedures?

Stored procedures are named, precompiled blocks of SQL and control-flow code saved in the database and executed on demand.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL STORED PROCEDURES: Supported by: SQL Server, MySQL, MariaDB, PostgreSQL, Oracle, DB2, Snowflake, Teradata. SQLite does not support stored procedures natively.

SQL STORED PROCEDURES Full Explanation

A stored procedure is a database object that bundles one or more SQL statements with optional procedural logic (IF, LOOP, variables) into a single callable unit. Because the code is parsed and optimized when it is created, execution is faster and network traffic is lower compared with sending raw SQL each time. Stored procedures enforce business rules close to the data, improve security through controlled execution rights, and promote code reuse. They can accept IN, OUT, and INOUT parameters, return result sets or status codes, and raise exceptions. Support and syntax vary by vendor, so always check your dialect’s documentation for nuances such as delimiter changes, language clauses, or permission requirements.

SQL STORED PROCEDURES Syntax

-- Creation
CREATE PROCEDURE procedure_name (
    @param1 datatype [IN | OUT | INOUT],
    @param2 datatype [IN | OUT | INOUT]
)
BEGIN
    -- SQL statements
END;

-- Execution
CALL procedure_name(value1, value2);

-- Removal
DROP PROCEDURE procedure_name;

SQL STORED PROCEDURES Parameters

  • procedure_name (Identifier) - The name of the procedure.
  • parameter_list (List) - Optional comma-separated parameters defined with mode (IN, OUT, INOUT) and data type.
  • body (Block) - One or more SQL statements enclosed by BEGIN ... END.

Example Queries Using SQL STORED PROCEDURES

-- 1. Create
CREATE PROCEDURE add_user (
    IN p_name  VARCHAR(100),
    IN p_email VARCHAR(255)
)
BEGIN
    INSERT INTO users(name, email) VALUES (p_name, p_email);
END;

-- 2. Call
CALL add_user('Ada Lovelace', 'ada@example.com');

-- 3. Create with OUT parameter
CREATE PROCEDURE user_count (OUT p_total INT)
BEGIN
    SELECT COUNT(*) INTO p_total FROM users;
END;

-- 4. Retrieve OUT value
CALL user_count(@total);
SELECT @total AS total_users;

Expected Output Using SQL STORED PROCEDURES

  • The first CREATE PROCEDURE statement stores the compiled code in the database.
  • The CALL adds a row to the users table and returns standard success status.
  • The second procedure stores the row count in the OUT parameter.
  • The final SELECT shows the number of rows in the users table.

Use Cases with SQL STORED PROCEDURES

  • Centralize complex business logic inside the database for speed and consistency
  • Reduce client-server round trips by grouping multiple statements
  • Enforce security by granting EXECUTE rights instead of direct table access
  • Provide stable APIs to application developers independent of schema changes
  • Batch data maintenance tasks such as nightly imports or cleanups

Common Mistakes with SQL STORED PROCEDURES

  • Forgetting to change the statement delimiter (e.g., MySQL requires DELIMITER $$) causing syntax errors
  • Omitting parameter mode, leading to default IN and unexpected read-only behavior
  • Assuming all dialects support RETURN like functions; many require OUT parameters instead
  • Neglecting to set proper privileges, which can expose or block needed access
  • Ignoring transaction scope; some dialects auto-commit inside procedures

Related Topics

CREATE PROCEDURE, Functions, Triggers, Views, Prepared Statements

First Introduced In

SQL:1999 (SQL Persistent Stored Modules)

Frequently Asked Questions

What is the difference between a stored procedure and a function?

A function must return a value and can be used in SELECT statements, while a stored procedure is executed with CALL/EXEC, can return zero or more result sets, and often performs data modifications.

How do I alter a stored procedure?

Use CREATE OR REPLACE PROCEDURE in PostgreSQL or run ALTER PROCEDURE/ALTER PROC in SQL Server. In MySQL, re-create it with DROP PROCEDURE followed by CREATE.

Can I debug stored procedures?

Yes. SQL Server Management Studio, MySQL Workbench, pgAdmin, and most IDEs provide step-through debugging if the database supports it.

Are stored procedures faster than prepared statements?

Execution speed is comparable once prepared, but stored procedures reduce repeated client preparation and network latency, often resulting in overall faster workflows.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!