MariaDB Stored Procedures: CREATE, CALL, and Parameters
A MariaDB stored procedure is a named SQL operation saved in a database. Define it with CREATE PROCEDURE and run it with CALL. It can accept inputs and produce result sets or output parameters. The privileges and transaction boundaries still need to be defined.
Prepare a test database and call a parameterless procedure
Create a learning database on a separate test server. The practice account needs CREATE, INSERT, SELECT, and UPDATE for the table and CREATE ROUTINE, EXECUTE, and ALTER ROUTINE for the procedures. If objects with these names already exist, use another practice environment. Run the examples in order in one connection using the mariadb client in regular MariaDB mode.
USE learning;
CREATE TABLE procedure_item_demo (
id INT PRIMARY KEY,
category CHAR(1) NOT NULL,
name VARCHAR(30) NOT NULL,
price DECIMAL(10,2) NOT NULL,
quantity INT NOT NULL
) ENGINE=InnoDB;
INSERT INTO procedure_item_demo VALUES
(1,'A','Notebook',10.00,2),
(2,'A','Pencil',2.00,5),
(3,'B','Ruler',5.00,3);
DELIMITER //
CREATE PROCEDURE learning.p_list_demo()
SQL SECURITY INVOKER
READS SQL DATA
BEGIN
SELECT id, name, quantity FROM learning.procedure_item_demo ORDER BY id;
END//
DELIMITER ;
CALL learning.p_list_demo();
The call returns (1, Notebook, 2), (2, Pencil, 5), and (3, Ruler, 3). The definition needs empty parentheses even with no parameters. The client command DELIMITER // lets the body contain semicolons and sends the full definition at //. Restore DELIMITER ; before calling it. With JDBC, PHP, or another driver API, send the full CREATE PROCEDURE as one SQL statement without the client delimiter lines. In graphical clients, follow the delimiter rules for the script runner. CREATE PROCEDURE syntax
Pass an IN value and capture OUT values
The next procedure takes a category and returns the number of products and the total inventory value. SELECT ... INTO assigns them to parameters. The previous example used a plain SELECT to emit a result set.
| Mode | Value flow | Typical use |
|---|---|---|
IN |
Caller to procedure | Filter or item ID |
OUT |
Procedure to caller | Count, total, or status |
INOUT |
Caller to procedure and back | Update a caller-supplied value |
DELIMITER //
CREATE PROCEDURE learning.p_category_summary_demo(
IN p_category CHAR(1),
OUT p_count INT,
OUT p_total DECIMAL(12,2)
)
SQL SECURITY INVOKER
READS SQL DATA
BEGIN
IF p_category IS NULL OR TRIM(p_category) = '' THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Category is required';
END IF;
SELECT COUNT(*), COALESCE(SUM(price * quantity), 0)
INTO p_count, p_total
FROM learning.procedure_item_demo
WHERE category = p_category;
END//
DELIMITER ;
CALL learning.p_category_summary_demo('A', @item_count, @total_value);
SELECT @item_count AS item_count, @total_value AS total_value;
CALL learning.p_category_summary_demo('C', @item_count, @total_value);
SELECT @item_count AS item_count, @total_value AS total_value;
Category A returns 2, 30.00. Category C has no products and returns 0, 0.00. @item_count and @total_value belong to the current session, so read them in the same connection that ran CALL. An OUT parameter does not carry the caller’s previous value into the procedure. Try calling this procedure separately with a NULL category to see SQLSTATE 45000. In applications, pass input through the driver’s parameter binding.
Update an existing value with INOUT
Use INOUT when the current value is needed for a calculation and the result should replace it. This example treats NULL as zero and adds one.
DELIMITER //
CREATE PROCEDURE learning.p_bump_demo(INOUT p_counter INT)
SQL SECURITY INVOKER
BEGIN
SET p_counter = COALESCE(p_counter, 0) + 1;
END//
DELIMITER ;
SET @visits = 10;
CALL learning.p_bump_demo(@visits);
SELECT @visits AS visit_count;
SET @visits = NULL;
CALL learning.p_bump_demo(@visits);
SELECT @visits AS visit_count;
The results are 11 and 1. Passing a literal as in CALL learning.p_bump_demo(10) fails because a literal cannot hold the updated value. In a SQL client, pass a user variable such as @visits. Inside another routine, a local variable or a parameter can also be used in the appropriate context. CALL and output parameters
Control the update and rollback boundary
The next procedure changes one row’s quantity without committing. The example accepts zero and rejects NULL and negative amounts. Call it within an explicit transaction and compare the quantity before and after rolling back.
DELIMITER //
CREATE PROCEDURE learning.p_set_quantity_demo(IN p_id INT, IN p_quantity INT)
SQL SECURITY INVOKER
MODIFIES SQL DATA
BEGIN
IF p_quantity IS NULL OR p_quantity < 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Quantity must be nonnegative';
END IF;
UPDATE learning.procedure_item_demo
SET quantity = p_quantity WHERE id = p_id;
END//
DELIMITER ;
START TRANSACTION;
CALL learning.p_set_quantity_demo(1, 9);
SELECT id, quantity FROM learning.procedure_item_demo WHERE id = 1;
ROLLBACK;
SELECT id, quantity FROM learning.procedure_item_demo WHERE id = 1;
The first query returns (1, 9). After rollback it returns (1, 2), because the InnoDB update was still uncommitted. BEGIN ... END groups statements. START TRANSACTION opens the transaction. An internal COMMIT or DDL that causes an implicit commit can change what the caller is able to roll back. A missing ID and an existing row whose value stays the same can both yield zero changed rows. Return a separate existence or status result if the caller needs to tell them apart. If a routine returns several result sets, consume all of them as your driver requires before sending another statement.
Inspect the definition and privileges before changing it
Back up the output of SHOW CREATE PROCEDURE and check the SQL mode under which the routine was created. With SQL SECURITY INVOKER, the caller needs the relevant table privileges in addition to EXECUTE. The default DEFINER runs the body with the definer account’s privileges. Give that account only the access the routine needs. If you cannot see the definition, check your permission to inspect it. Stored routine privileges and security context
SHOW PROCEDURE STATUS WHERE Db = 'learning';
SHOW CREATE PROCEDURE learning.p_category_summary_demo;
SELECT ROUTINE_NAME, SECURITY_TYPE, DEFINER
FROM information_schema.ROUTINES
WHERE ROUTINE_SCHEMA = 'learning' AND ROUTINE_TYPE = 'PROCEDURE'
ORDER BY ROUTINE_NAME;
ALTER PROCEDURE changes properties such as a comment or the security context. Changing parameters or the body requires a new definition. The examples below update the comment and then keep the same calling signature while changing the increment to two. Properties supported by ALTER PROCEDURE
ALTER PROCEDURE learning.p_bump_demo COMMENT 'Counter increment example';
SHOW CREATE PROCEDURE learning.p_bump_demo;
DELIMITER //
CREATE OR REPLACE PROCEDURE learning.p_bump_demo(INOUT p_counter INT)
SQL SECURITY INVOKER
BEGIN
SET p_counter = COALESCE(p_counter, 0) + 2;
END//
DELIMITER ;
SET @visits = 10;
CALL learning.p_bump_demo(@visits);
SELECT @visits AS visit_count;
The final result is 12. MariaDB’s CREATE OR REPLACE preserves privileges granted on the routine. Dropping it separately before recreating it can affect those grants. Check the definition, routine grants, and dependent calls before using either approach.
Drop the practice procedures
Confirm the database names, routine names, and saved definitions before dropping. DROP PROCEDURE takes the routine name without parentheses. DROP PROCEDURE syntax
DROP PROCEDURE learning.p_list_demo;
DROP PROCEDURE learning.p_category_summary_demo;
DROP PROCEDURE learning.p_bump_demo;
DROP PROCEDURE learning.p_set_quantity_demo;
SELECT COUNT(*) AS remaining_demo_procedures
FROM information_schema.ROUTINES
WHERE ROUTINE_SCHEMA = 'learning'
AND ROUTINE_NAME IN ('p_list_demo','p_category_summary_demo',
'p_bump_demo','p_set_quantity_demo');
SELECT id, quantity FROM learning.procedure_item_demo ORDER BY id;
remaining_demo_procedures is zero. The quantities in the table are still 2, 5, and 3. Dropping the procedure removes its definition. The table and any already committed data changes remain, so plan definition restoration and data recovery separately.




