Stored Procedures in SQL

From the SQL cheat sheet · Views & Stored Procedures · verified Jul 2026

Stored Procedures

Save reusable blocks of SQL logic on the database server.

sql
-- Create procedure (MySQL)
DELIMITER //
CREATE PROCEDURE get_user_orders(IN p_user_id INT)
BEGIN
  SELECT * FROM orders WHERE user_id = p_user_id;
END //
DELIMITER ;

-- Call it
CALL get_user_orders(42);
💡 Syntax varies significantly: MySQL uses DELIMITER, PostgreSQL uses $$ blocks, SQL Server uses @params
⚡ Stored procedures run on the server — reduce network round trips for complex operations
📌 PostgreSQL uses CREATE FUNCTION (not PROCEDURE) for most use cases
🟢 Use stored procedures for operations that should always run the same logic regardless of client
stored-proceduresfunctionsplpgsql

More SQL tasks

Back to the full SQL cheat sheet