PROCEDURE represents a reusable, named block of SQL and procedural code that is stored inside the database catalog. Like functions, procedures allow developers to centralize business logic close to the data, reduce duplication, and improve performance by avoiding repeated parsing and network round-trips. A procedure can accept input, output, or input-output parameters, perform complex operations (loops, conditions, transactions), and optionally return status information. Unlike functions, procedures typically do not return a value in an expression context; they are invoked for their side effects using CALL (MySQL, PostgreSQL) or EXEC/EXECUTE (SQL Server, Oracle).Behavior varies by dialect:- MySQL and MariaDB support CREATE PROCEDURE with deterministic and SQL-security modifiers.- SQL Server implements stored procedures with CREATE PROCEDURE and supports T-SQL.- PostgreSQL introduced CREATE PROCEDURE in version 11; before that, developers used CREATE FUNCTION with "RETURNS void".- Oracle PL/SQL uses CREATE PROCEDURE inside a PL/SQL block or package.Important caveats:- Permissions: executing or altering a procedure requires explicit privileges (e.g., EXECUTE, ALTER ROUTINE).- Parameter style and delimiter characters differ between dialects.- Debugging and error-handling mechanisms (e.g., EXCEPTION, TRY...CATCH) are language specific.- Recursive or long-running procedures can lock resources if not carefully coded.
procedure_name
(identifier) - Name of the stored procedure.parameter_list
(list) - Zero or more IN, OUT, or INOUT/OUTPUT parameters with data types.routine_body
(block) - SQL and control-flow statements executed when the procedure is called.characteristics
(optional) - Modifiers such as LANGUAGE, DETERMINISTIC, SQL SECURITY. If not supported, specify None for that dialect.CREATE PROCEDURE, FUNCTION, CALL, EXEC, BEGIN...END, DECLARE, CURSOR
SQL:1999 (SQL/PSM); MySQL 5.0
You need the EXECUTE privilege on the procedure or the database. If the procedure performs data changes, you also need privileges on the underlying tables unless the procedure is defined with SQL SECURITY DEFINER (MySQL) or executed as a signed module (SQL Server).
Yes. In MySQL and SQL Server you can issue multiple SELECT statements inside the body, and each produces its own result set that the client can consume sequentially.
Use ALTER PROCEDURE where supported (SQL Server) or drop and recreate the procedure with CREATE OR REPLACE (PostgreSQL) or DROP PROCEDURE followed by CREATE PROCEDURE (MySQL).
They run within the caller's transaction context unless they start or end transactions explicitly. In MySQL you can include START TRANSACTION and COMMIT inside the procedure; in SQL Server you use BEGIN TRANSACTION. Always handle rollbacks to avoid partial updates.