MySQL Error 1172 - Result consisted of more than one row - mysql

I'm getting this error from MySQL when running a query inside a stored procedure:
Error Code: 1172
Result consisted of more than one row
I understand the error: I'm doing a SELECT (...) INTO (var list), and thus the query is required to return a single row. When I use LIMIT 1 or SELECT DISTINCT, the error goes away.
However: when I run the original query manually (without LIMIT or DISTINCT), it does return a single row. So I'm suspecting I may have bumped into a MySQL bug. Does anyone know what could be happening?
EDIT
I'm posting the SQL as requested. Everything that starts with an underscore is a variable declared earlier inside the procedure. When I test it, I'm replacing _cd_pai_vc with the ID for the record that is causing the problem.
SELECT a.valor, IFNULL(p.valor, 0), fn_cd2alias(ra.cd_registro), fn_cd2alias(IFNULL(p.valor,0))
INTO _valor, _cd_pai_vc, _alias_verbete, _alias_pai
FROM dados_registros ra
INNER JOIN dados_varchar255 a
ON a.cd_registro = ra.cd_registro
AND a.fl_excluido = 0
AND a.alias = 'vc-verbetes-termo'
LEFT OUTER JOIN dados_registros rp
INNER JOIN dados_int p
ON p.cd_registro = rp.cd_registro
AND p.fl_excluido = 0
AND p.alias = 'vc-remissoes-termo referenciado'
INNER JOIN dados_int pt
ON pt.cd_registro = rp.cd_registro
AND pt.fl_excluido = 0
AND pt.alias = 'vc-remissoes-tipo remissao'
AND fn_cd2alias(pt.valor) = 'hierarquica'
ON ra.cd_registro = rp.cd_entidade
AND rp.fl_excluido = 0
AND fn_cd2alias(rp.cd_modulo) = 'vc-remissoes'
WHERE ra.cd_registro = _cd_pai_vc
AND ra.fl_excluido = 0;

I had the similiar issue and when I put table alias it worked like a charm.
SELECT t.tax_amount,t.tax_percentage FROM nepse_tax t

I had this problem and found it went away when I used both table name and column name in select statements, even simple ones.

The issue i had was IN parameter and the column name both were same so altered IN parameter name and it worked

I have experienced the same error in mysql.
MySQL Error 1172 - Result consisted of more than one row
Then I saw the question:
mysql stored procedure error (1172, 'Result consisted of more than one row')
But it was not I meant to ask. LIMIT 1;-wasn't up to my expectation. It will just return the first row for all the case.
Then I started looking at this one deeply and now I got the solution.
This case happend because the code for stored procedure returns multiple rows and that was because I had many extra spaces and tab characters in my code[the code I wrote for stored procedure] and when I removed them with just one/two appropriate tab chars- it was just like a flying machine.
I don't know if it is the same case that you are experienced with. Anyway give a try.
Thank you.

I had the same problem, the solution by Nava Bogatee worked like a charm,
below is a part of the procedure
select attendance
into brb
from Stud
where Rno like rno;
This didn't work I have no idea why.
Then I changed this to
select t.attendance
into brb
from Stud t
where t.Rno like rno;
and it worked.
I guess due to some reason the Rno column is returning all the values in the table, that is why the multiple row error.

Related

MySQL Stored Procedure with Parameters for Recursive CTE

I'm working on a MySQL Way of printing an "affiliate tree" and got the thing working with Common Table Expression. I'm using the following code right now:
WITH RECURSIVE recUsers AS
(
SELECT ID, username, sponsorID, 1 AS depth, username AS path
FROM users
WHERE id = 1
UNION ALL
SELECT c.ID, c.username, c.sponsorID, sc.depth + 1, CONCAT(sc.path, ' > ', c.username)
FROM recUsers AS sc
JOIN users AS c ON sc.ID = c.sponsorID
)
SELECT * FROM recUsers;
This selects the tree underneath the user with the id 1.
Now what I'd need to get is a way to pass that id as a parameter, so I don't need to define everything from the beginning every time I want to get the result.. So my idea is to put everything in a stored prodecure and pass the id in as a parameter.. However, so far I didn't get it working and always getting various errors that are very self speaking...
Basically what I've tried was
DELIMITER //
CREATE PROCEDURE getAffiliateTree(IN userid INT())
BEGIN
---my code here, the userid 1 replaced with userid
END//
DELIMITER;
However, this doesn't seem to work.. How can I get this done?
Two things I would suggest:
Use INT, not INT(). The optional length argument to integer types is deprecated in MySQL 8.0 (which I know you're using, because you're using CTE syntax). Even if you did use the length argument, using an empty argument is not legal syntax.
Make sure that the userid input parameter name is distinct from all of the columns in the tables you reference. That is, if the table has a column named userid (any capitalization), then change the name of your input parameter. Otherwise you may make ambiguous expressions like:
... WHERE userid = userid
Even though you intend one of these to be the column and the other to be the parameter, the SQL parser has no way of knowing that. It ends up treating both as the column name, so it's trivially true on all rows of the table.
Actually, a third thing I would suggest: when you ask questions, "it doesn't seem to work" isn't clear enough. Did it produce an error? If so, what was the full error message? Did it produce no error, but didn't give you the result you wanted? If so, show a mocked-up example of what you expected, and what the query produced that didn't match. It helps to be as clear as you can when you post questions, so readers don't have to guess what trouble you need help with.

OPENQUERY SQL Server MYSQL UPDATE

I have to work on a linked server. My goal: Update an entire table in mysql server(version:8.0.21) via OPENQUERY in SQL Server(version 13.0.1742.0). I tried this but it generates an error Row cannot be located for updating. Some values may have been changed since it was last read and this one The rowset was using optimistic concurrency and the value of a column has been changed after the containing row was last fetched or resynchronized.
update linkedTable
set
linkedTable.id_parent=unlinkedTable.IdCat1,
linkedTable.code=unlinkedTable.CodeFamilleFAT,
linkedTable.niveau=unlinkedTable.NiveauCategorieFAT,
linkedTable.langue=unlinkedTable.CodeLangueFAT,
linkedTable.nom=unlinkedTable.LibelleCommercialFAT,
linkedTable.descriptionA=unlinkedTable.DescriptifCom1FAT,
linkedTable.vignette=null,
linkedTable.id_categorie=unlinkedTable.id
from openquery(NAMELINKEDSERVER, 'select id_categorie, id_parent, code, niveau, langue, nom, description as descriptionA, vignette from DatabaseMySQL.Table') as linkedTable
inner join DatabaseSQLserver.dbo.Table as unlinkedTable on unlinkedTable.Id = linkedTable.id_categorie
Then I tried this:
update linkedTable
set
linkedTable.id_parent=unlinkedTable.IdCat1,
linkedTable.code=unlinkedTable.CodeFamilleFAT,
linkedTable.niveau=unlinkedTable.NiveauCategorieFAT,
linkedTable.langue=unlinkedTable.CodeLangueFAT,
linkedTable.nom=unlinkedTable.LibelleCommercialFAT,
linkedTable.descriptionA=unlinkedTable.DescriptifCom1FAT,
linkedTable.vignette=null,
linkedTable.id_categorie=unlinkedTable.id
from openquery(NAMELINKEDSERVER, 'select id_categorie, id_parent, code, niveau, langue, nom, description as descriptionA, vignette from DatabaseMySQL.Table') as linkedTable
inner join DatabaseSQLserver.dbo.Table as unlinkedTable on unlinkedTable.Id = linkedTable.id_categorie
where linkedTable.id_categorie = 1
This work but only one row is updated. So I wrote a stored procedure to update each line but it took too much time.
Can someone explain why my first query didn't work (question1) and how I can reduce the time of my stored procedure (question2)?
I use while loop (count the number of id and update each id).
Thank you in advance.
Kind Regards.
I resolve the problem by checking some option on ODBC Driver in MySQL and reading some forum. I check this box.
enter image description here
This option allows to avoid the errors quoted previously. With this option, i can update multiple values without error on join or other request. Thank you Solarflare and "Another guy" (i lost the name) for correcting me (EDIT A POST). Have nice day both.

Statement in trigger is not "picking up" the condition in its Where clause

so I'm currently working on a MySQL trigger. I'm trying to assign values to two variables when a new record is inserted. Below are the queries:
SET mssgDocNo = (SELECT Document_ID FROM CORE_MSSG WHERE Message_ID = new.MSSG_ID);
SET mssgRegime = (SELECT CONCAT (Regime_Type, Regime_Code) FROM T_DOC WHERE CD_Message_ID = new.MSSG_ID);;
For some reason, the second SQL query is not picking up the 'new.MSSG_ID' condition while the first query in same trigger recognizes it. I really can't figure out what seems to be the problem.
When I replace the 'new.MSSG_ID' with a hard-coded value from the database in the second query it seems to work. I doubt the 'new.MSSG_ID' is the problem because it works perfectly fine in the first query.
I've tried pretty much anything I could think of. Would appreciate the help.
I would write these more simply as:
SELECT mssgDocNo := Document_ID
FROM CORE_MSSG
WHERE Message_ID = new.MSSG_ID;
SELECT mssgRegime := CONCAT(Regime_Type, Regime_Code)
FROM T_DOC
WHERE CD_Message_ID = new.MSSG_ID;
The SET is not necessary.
I did make one other change that might make it work. I removed the space after CONCAT. Some versions of MySQL have trouble parsing spaces after function calls.

what is wrong with this line of SQL?

SELECT Batch.NumStud
FROM Batch
WHERE CourseID='$courseid'
INNER JOIN Course
ON Batch.CourseID=Course.CourseID"
an error that says mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>F:\AppServ\www\anNoECourse.php
is shown.This code was written to feed in data to a google chart.
You put SQL in wrong order (JOIN and WHERE are switched):
SELECT Batch.NumStud
FROM Batch INNER JOIN Course
ON Batch.CourseID = Course.CourseID
WHERE Course.CourseID = '$courseid'
It seems, that your query can be simplified (check your data):
select Batch.NumStud
from Batch
where Batch.CourseID = '$courseid'
I think the error is a bit more complex. Due to the fact that your SQL is invalid, you're not getting a result set. This case is not handled correctly by your PHP code!
So in addition to correcting your SQL as the others have suggested, please make sure to handle the case where you get no results or your query results in an error correctly in your PHP code!
The second part to your solution is as follows:
$result = mysql_query(...);
if ($result)
{
while (...)
...
}
This makes sure that mysql_query actually returned a result set and not false, which it does in case of errors (due to your invalid SQL code, but also in other cases). So just fixing your SQL is not enough to make your script error proof.
But again, do no longer use the mysql_.... functions! They are deprecated.

Mysql OR query with a join returns an out of memory error

I have a query that tries to tell is a member had participated in any of 3 types of actions. Each action is represented by a table that I am joining. And then I am doing an or to see if there is a match in any of the tables. Here is the query:
select problems.problem_id, problem_title from
problems left join attempted_solutions
on roblems.creator_member_id = attempted_solutions.suggester_id
left join problem_comments
on problems.creator_member_id = problem_comments.user_id
left join suggested_solutions
on problems.creator_member_id = suggested_solutions.suggester_id
where attempted_solutions.suggester_id = 1
or problem_comments.user_id = 1 or suggested_solutions.suggester_id = 1;
But for some reason this gives a "MySQL client ran out of memory error." if I try it command-line, and a "database connection interrupted" error if I do it from a script.
Any idea where I am going wrong here?
This thread has some solutions maybe you should try them see if any of them is your problem
http://forums.mysql.com/read.php?37,94800,232288#msg-232288
also, try to simplify your query then add your joins and where statements back one by one so that you can detect the exact cause of your problem