CREATE PROCEDURE is a Data Definition Language (DDL) statement that registers executable logic inside the database. A procedure encapsulates one or more SQL statements (and optional procedural code) so that they can be called by name, often with IN, OUT, or INOUT parameters. Stored procedures run on the server, close to the data, which can reduce network traffic, centralize business rules, and improve performance.Most products implement CREATE PROCEDURE as part of their procedural extensions (PL/pgSQL for PostgreSQL, T-SQL for SQL Server, PL/SQL for Oracle, etc.). Core behavior is similar: you supply a unique procedure name, optional parameter list, and a block of statements terminated with END (or language-specific keyword). Procedures can return data through OUT parameters, result sets (SELECT inside), or database-specific RETURN/RESULT constructs.Key points:- Requires appropriate privileges (e.g., CREATE ROUTINE in MySQL, CREATE PROCEDURE in SQL Server).- Stored in the system catalog until explicitly dropped.- May contain transactional control statements depending on dialect.- Parameter modes default to IN when not specified.- Changing an existing procedure usually requires DROP PROCEDURE then CREATE, unless ALTER PROCEDURE is supported.- In MySQL CLI you must change the statement delimiter so the body’s semicolons are not misinterpreted.
procedure_name
(identifier) - Name of the procedure to create.param_mode
(keyword) - IN, OUT, or INOUT/OUTPUT indicating direction (default IN).param_name
(identifier) - Name of each parameter.data_type
(type) - Valid SQL data type for each parameter.LANGUAGE
(keyword) - Procedural language in which the body is written (dialect specific).DETERMINISTIC
(flag) - Indicates the routine always returns the same result for the same inputs (MySQL and others).Body
(block) - One or more SQL statements enclosed by BEGIN … END or dialect-specific wrapper.CREATE FUNCTION, CALL, EXEC, DROP PROCEDURE, ALTER PROCEDURE, Stored Functions, Triggers, Transactions
SQL-99 (Persistent Stored Modules)
A procedure may return data via OUT parameters or result sets but does not have to return a scalar value, while a function must return a value and can often be used inside SELECT statements.
Yes, most dialects allow BEGIN/COMMIT/ROLLBACK inside procedures, but behavior varies. SQL Server treats the procedure as part of the caller’s transaction unless a new one is started explicitly.
Options include printing messages (RAISE NOTICE, PRINT), stepping through with an IDE debugger (SQL Server Management Studio, MySQL Workbench), or logging to a table.
They can be because execution happens on the server, reducing round-trips and benefitting from cached execution plans. Actual performance depends on workload and design.