Error
Error Code:
3953
MySQL Error 3953: Unexpected Multiple INTO Clauses
Description
This error indicates that a single query block, such as a `SELECT ... INTO` statement, contains more than one `INTO` clause. MySQL syntax dictates that only one `INTO` clause is allowed per query block for assigning query results to variables.
Error Message
Multiple INTO clauses in one query block.
Known Causes
3 known causesAccidental Duplication
A typographical error or an inadvertent copy-paste operation resulted in an extra INTO clause within the same query block.
Incorrect Syntax for Multiple Assignments
Attempting to assign multiple variables using separate INTO clauses in a single `SELECT` statement, rather than specifying all variables in one `INTO` clause.
Query Refactoring Oversight
During the modification or merging of complex queries, an additional INTO clause was unintentionally included or left in the final statement.
Solutions
3 solutions available1. Remove Redundant INTO Clauses easy
Identify and delete extra INTO clauses from your SQL statement.
1
Carefully examine your SQL query. The error 3953 indicates that there are multiple `INTO` keywords within a single query block. This typically occurs in statements like `SELECT ... INTO ... INTO ...` or within stored procedures/functions where `SELECT ... INTO` is used multiple times without proper separation.
SELECT column1, column2 INTO @var1, @var2 FROM my_table WHERE condition;
-- This is the problematic part if there's another INTO clause following.
2
Determine which `INTO` clause is the intended one. If you are assigning values to user-defined variables, ensure only one `SELECT ... INTO` statement is present per logical block of code. If you are inserting data into a table, ensure you are not accidentally using `INTO` in a `SELECT` statement that is not intended for data insertion.
SELECT column1, column2 INTO @var1, @var2 FROM my_table WHERE condition;
-- If you intended to insert into another table, use a separate INSERT statement:
INSERT INTO another_table (col_a, col_b) SELECT column1, column2 FROM my_table WHERE condition;
3
Delete or comment out any superfluous `INTO` clauses. If the query is intended to return a result set, remove all `INTO` clauses. If it's for variable assignment, keep only one. If it's for table insertion, ensure the syntax is correct for `INSERT ... SELECT`.
SELECT column1, column2 FROM my_table WHERE condition; -- Removed redundant INTO clause
2. Restructure Stored Procedures/Functions medium
Ensure each SELECT INTO statement is properly scoped and separated within procedural code.
1
If the error occurs within a stored procedure, function, or trigger, review the procedural logic. The `SELECT ... INTO` statement is often used to assign values to local or user-defined variables. Multiple such statements can lead to this error if they are not correctly separated or if there's an unintended duplicate.
DELIMITER //
CREATE PROCEDURE example_proc(IN input_val INT)
BEGIN
DECLARE var1 INT;
DECLARE var2 INT;
-- Problematic: Two SELECT INTO statements in a row without proper logic separation
SELECT column_a INTO var1 FROM my_table WHERE id = input_val;
SELECT column_b INTO var2 FROM another_table WHERE related_id = input_val;
-- ... rest of the procedure
END //
DELIMITER ;
2
Ensure that each `SELECT ... INTO` statement is logically distinct. If you need to populate multiple variables, you can sometimes combine them into a single `SELECT ... INTO` if the source columns align. Alternatively, ensure that intermediate operations or conditional logic correctly separates the `SELECT INTO` statements.
DELIMITER //
CREATE PROCEDURE example_proc(IN input_val INT)
BEGIN
DECLARE var1 INT;
DECLARE var2 INT;
-- Corrected: If possible, combine into one SELECT INTO
SELECT column_a, column_b INTO var1, var2 FROM my_table WHERE id = input_val;
-- Or, if they must be separate, ensure logical separation (e.g., IF statements, different queries)
-- SELECT column_a INTO var1 FROM my_table WHERE id = input_val;
-- IF var1 IS NOT NULL THEN
-- SELECT column_b INTO var2 FROM another_table WHERE related_id = var1; -- Example using var1
-- END IF;
-- ... rest of the procedure
END //
DELIMITER ;
3
If you are using `SELECT ... INTO` to insert data into a temporary table or a result set within a cursor, ensure that the `INTO` clause is used only once for that specific operation and that it's not mixed with other `SELECT ... INTO` statements meant for variable assignment.
DELIMITER //
CREATE PROCEDURE example_proc_cursor(IN input_val INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE temp_col INT;
DECLARE cur CURSOR FOR SELECT column_a FROM my_table WHERE id = input_val;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO temp_col; -- Correct usage within a cursor loop
IF done THEN
LEAVE read_loop;
END IF;
-- Process temp_col
END LOOP;
CLOSE cur;
END //
DELIMITER ;
3. Correct INSERT Statement Syntax easy
Ensure INSERT statements are not mistakenly using SELECT INTO syntax.
1
This error can also arise if you are trying to insert data into a table and have incorrectly used the `SELECT ... INTO` syntax instead of the standard `INSERT ... SELECT` syntax.
INSERT INTO target_table (col1, col2)
SELECT column_a, column_b
FROM source_table
WHERE condition; -- This is the correct syntax for inserting from a SELECT statement.
2
Compare your statement with the correct `INSERT ... SELECT` syntax. The `INSERT` keyword should precede the `target_table` and its column list, and the `SELECT` statement follows, providing the data.
INSERT INTO target_table (col1, col2)
INTO source_table (column_a, column_b) -- INCORRECT: This is not valid syntax for INSERT
WHERE condition;
3
Replace any incorrect `INTO` clauses within an `INSERT` statement with the standard `SELECT` statement. Ensure there is only one `SELECT` statement providing the data for the `INSERT` operation.
INSERT INTO target_table (col1, col2)
SELECT column_a, column_b FROM source_table WHERE condition;
-- Removed the erroneous second INTO clause.