这个报错太具有误导性了,致使我们花了好长时间,采用了很多错误的解决方案。
项目中有一个表,有id、qr_code、parent_qr_code这3个字段,层级没有限制。有时候需要在代码中查询某个qr_code的顶级qr_code,用代码写递归查询感觉效率会比较差,所以写了个函数来查root节点:
sql
BEGIN
DECLARE current_qrcode VARCHAR(255);
DECLARE parent_qrcode VARCHAR(255);
SET current_qrcode = start_qrcode;
-- 防止无限循环,设置最大层级
SET @max_level = 100;
SET @current_level = 0;
loop_label: LOOP
SET @current_level = @current_level + 1;
-- 如果超过最大层级,退出循环
IF @current_level > @max_level THEN
LEAVE loop_label;
END IF;
-- 查询当前记录的父级
SELECT t.parent_qr_code INTO parent_qrcode
FROM qr_tree t
WHERE t.qr_code = current_qrcode;
-- 如果没有父级或者父级为空,说明找到顶级
IF parent_qrcode IS NULL OR parent_qrcode = '' THEN
LEAVE loop_label;
ELSE
SET current_qrcode = parent_qrcode;
END IF;
END LOOP;
RETURN current_qrcode;
END
在测试环境之前走的一切正常,但是今天上生产环境突然报错:
shell
### Cause: java.sql.SQLSyntaxErrorException: Result consisted of more than one row
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Result consisted of more than one row
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Result consisted of more than one row
### The error may exist in URL [jar:file:/home/PROJECT/spring-boot-system.jar!/BOOT-INF/lib/spring-boot-business.jar!/org/qrTree/mapper/xml/QrTree.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from qr_tree where qr_code = (SELECT find_top_qrcode(?)) limit 1
### Cause: java.sql.SQLSyntaxErrorException: Result consisted of more than one row
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Result consisted of more than one row
这个报错太具有误导性了,一开始一看到这个报错,就觉得是函数或者整条SQL返回了多条数据,而只能接收一条所报的错,于是在Navicat里各种执行,发现都没问题,但是在项目里就是会报错。
代码改来改去都不行,不如给函数后面加个LIMIT 1,或者去掉嵌套式的查询直接qr_code=find_top_qrcode(),结果发现依然报错。
然后就给函数里面的查询加了个LIMIT 1,就是这一段:
sql
SELECT t.parent_qr_code INTO parent_qrcode
FROM qr_tree t
WHERE t.qr_code = current_qrcode LIMIT 1;
然后发现果然不报错了。
就在我们以为万事大吉之后,发现了另一个问题:这个业务操作之后,这个表中的qr_code列会出现重复。
经过代码的梳理,我发现我本来写的是replace info,因为数据库的ID没有自增,所以手动设置了UUID,这时候就发现问题了,生产环境的数据库没!有!设!置!唯!一!键!
如果设置了唯一键,这个表的qr_code就不可能重复。那么加上唯一键,刚才的报错会消失吗?
我加上了唯一键,并且去掉了刚才胡乱添加的LIMIT 1,再跑一遍代码,果然,不报错了!