Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-29 · container image mysql:8.4
SELECT ... INTO stores one row into variables. If the query returns two or more rows, MySQL stops with ERROR 1172 instead of picking one. It happens the same way inside a stored procedure and with a user variable like @price.
Below are three cases verified on MySQL 8.4: a procedure whose WHERE matches several rows, a direct SELECT ... INTO @var, and a lookup key that has duplicate data. In each case we first counted the matching rows, then made the query return exactly one row.
01SELECT INTO inside a procedure matches several rows
Procedure get_price runs SELECT price INTO p_price FROM products WHERE category = p_cat;. Category 'A' has two rows (id 1 = 100, id 2 = 150), so CALL get_price('A', @price); fails with ERROR 1172.
We recreated the procedure with ORDER BY id DESC LIMIT 1. The same CALL then succeeded and @price was 150, the price of the newest row.
Setup
CREATE TABLE products (id INT PRIMARY KEY, category VARCHAR(10), price INT);
INSERT INTO products VALUES (1, 'A', 100), (2, 'A', 150), (3, 'B', 300);
CREATE PROCEDURE get_price(IN p_cat VARCHAR(10), OUT p_price INT)
SELECT price INTO p_price FROM products WHERE category = p_cat;Query OK, 0 rows affected (0.01 sec)
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
Query OK, 0 rows affected (0.00 sec)CALL get_price('A', @price);ERROR 1172 (42000): Result consisted of more than one rowSELECT id, category, price FROM products WHERE category = 'A';+----+----------+-------+
| id | category | price |
+----+----------+-------+
| 1 | A | 100 |
| 2 | A | 150 |
+----+----------+-------+
2 rows in set (0.00 sec)DROP PROCEDURE get_price;
CREATE PROCEDURE get_price(IN p_cat VARCHAR(10), OUT p_price INT)
SELECT price INTO p_price FROM products WHERE category = p_cat ORDER BY id DESC LIMIT 1;Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)LIMIT 1 without ORDER BY returns an arbitrary row. Always state which row you want.
CALL get_price('A', @price);
SELECT @price AS price;Query OK, 1 row affected (0.00 sec)
+-------+
| price |
+-------+
| 150 |
+-------+
1 row in set (0.00 sec)02SELECT INTO @variable returns several rows
No procedure is needed. SELECT price INTO @price FROM products WHERE category = 'A'; gives the same ERROR 1172, and COUNT(*) showed 2 matching rows.
If you actually want one summary value, aggregate it. SELECT MAX(price) INTO @price ... always returns one row and stored 150.
Setup
CREATE TABLE products (id INT PRIMARY KEY, category VARCHAR(10), price INT);
INSERT INTO products VALUES (1, 'A', 100), (2, 'A', 150), (3, 'B', 300);Query OK, 0 rows affected (0.01 sec)
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0SELECT price INTO @price FROM products WHERE category = 'A';ERROR 1172 (42000): Result consisted of more than one rowSELECT COUNT(*) AS cnt FROM products WHERE category = 'A';+-----+
| cnt |
+-----+
| 2 |
+-----+
1 row in set (0.00 sec)SELECT MAX(price) INTO @price FROM products WHERE category = 'A';
SELECT @price AS max_price;Query OK, 1 row affected (0.00 sec)
+-----------+
| max_price |
+-----------+
| 150 |
+-----------+
1 row in set (0.00 sec)03Duplicate data in a column that should be unique
SELECT name INTO @name FROM customers WHERE code = 'C001'; assumes customer codes are unique, but the table had no constraint and C001 was inserted twice. The GROUP BY ... HAVING COUNT(*) > 1 check found C001 with 2 rows.
We deleted the duplicate row (id 3) and added UNIQUE KEY customers_code_uk (code). The lookup then returned Kim, and the unique key stops the duplicate from coming back.
Setup
CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, code VARCHAR(10), name VARCHAR(20));
INSERT INTO customers (code, name) VALUES ('C001', 'Kim'), ('C002', 'Lee'), ('C001', 'Kim');Query OK, 0 rows affected (0.02 sec)
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0SELECT name INTO @name FROM customers WHERE code = 'C001';ERROR 1172 (42000): Result consisted of more than one rowSELECT code, COUNT(*) AS cnt FROM customers GROUP BY code HAVING COUNT(*) > 1;+------+-----+
| code | cnt |
+------+-----+
| C001 | 2 |
+------+-----+
1 row in set (0.00 sec)DELETE FROM customers WHERE id = 3;
ALTER TABLE customers ADD UNIQUE KEY customers_code_uk (code);Query OK, 1 row affected (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0Here the duplicate was a full copy, so deleting it was safe. With real data, check which row is correct before deleting.
SELECT name INTO @name FROM customers WHERE code = 'C001';
SELECT @name AS name;Query OK, 1 row affected (0.00 sec)
+------+
| name |
+------+
| Kim |
+------+
1 row in set (0.00 sec)FAQ
What does "Result consisted of more than one row" mean in MySQL?
A SELECT ... INTO returned two or more rows. Variables can hold only one row, so MySQL raises ERROR 1172 instead of choosing one.
How do I fix ERROR 1172 in a stored procedure?
First run the same SELECT without INTO and count the rows. Then make it return one row: narrow the WHERE, add ORDER BY ... LIMIT 1, or use an aggregate like MAX(). If the data should be unique, remove the duplicates and add a UNIQUE key.
Does ERROR 1172 happen outside procedures?
Yes. A plain SELECT price INTO @price ... in a normal session gave the same error when two rows matched.
Is LIMIT 1 a safe fix?
Only with ORDER BY that picks the row you really want. If the extra rows are bad data, LIMIT 1 hides the problem; fix the data and add a constraint instead.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13