mysqldude

Stored procedures with OUT parameters

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
CALL with an OUT parameter into @s (original)
original post
✓✓✓✓✓✓✓✓✓
Stored function returning TINYINT, as the post suggests (READS SQL DATA)
modern rewrite
✓✓✓✓✓✓✓✓✓
CALL with an OUT parameter into @s (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
Stored function returning TINYINT, as the post suggests (READS SQL DATA) modern rewrite
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9

The original article

First published 2011-03-08. The text below is the original; the table above shows how it behaves on current versions.

This post provides sample code of a stored procedure that includes an OUT parameter. The basic concept is something to validate a username and password combination. A better way of doing this would be to write a stored function which returns a TINYINT or BIT to indicate success or failure.

DROP TABLE IF EXISTS logins;

CREATE TABLE logins
  (
     id       INT AUTO_INCREMENT,
     username VARCHAR(100),
     PASSWORD VARCHAR(100),
     PRIMARY KEY (id)
  );

INSERT INTO logins
            (username,
             PASSWORD)
VALUES      ('test',
             'test123');

DROP PROCEDURE IF EXISTS pValidate;

DELIMITER $$
CREATE PROCEDURE pValidate(IN pUsername VARCHAR(100), IN pPassword VARCHAR(100), OUT pResult VARCHAR(100))
BEGIN
	DECLARE count INT;
	SELECT count(1) INTO count FROM logins WHERE username = pUsername AND password = pPassword;
	IF count > 0 THEN
		SET pResult = 'success';
	ELSE
		SET pResult = 'failed';
	END IF;
END;
$$
DELIMITER ;

SET @s = "";
CALL pValidate('test','test321', @s);
SELECT @s;

CALL pValidate('test','test123', @s);
SELECT @s;

The result of this code is:

mysql> source proc.sql
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.04 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+--------+
| @s     |
+--------+
| failed |
+--------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+---------+
| @s      |
+---------+
| success |
+---------+
1 row in set (0.00 sec)