服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - SQL模糊查询报:ORA-00909:参数个数无效问题的解决

SQL模糊查询报:ORA-00909:参数个数无效问题的解决

2021-08-21 21:36dqcer Mysql

这篇文章主要介绍了SQL模糊查询报:ORA-00909:参数个数无效问题的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

用oracle数据库进行模糊查询时,

控制台报错如下图所示:

SQL模糊查询报:ORA-00909:参数个数无效问题的解决

原因是因为敲的太快,语法写错了

正确的写法是

?
1
pd.code like concat(concat('%',#{keyword}),'%')

java.sql.SQLSyntaxErrorException: ORA-00909: 参数个数无效

用MyBatis进行多参数模糊查询的时候遇到这个异常,看了下打印日志,发现异常出在预编译之后,插入实参的时候。

==> Preparing: select role_id, role_name, note from t_role where role_name like concat('%', ?, '%') and note like concat('%', ?, '%')
2018-12-13 20:24:28,567 DEBUG [com.ss.learn.chapter3.mapper.RoleMapper.getRolesByIdAndNote] - ==> Parameters: 1(String), 1(String)

异常提示:参数个数无效。检查了下SQL语句

?
1
2
select role_id, role_name, note from t_role
where role_name like concat('%', ?, '%') and note like concat('%', ?, '%')

发现问题出现在concat上,concat是连接两个字符串的函数,这里连接了三个,把SQL改成两个concat嵌套的

?
1
2
3
4
5
<select id="getRolesByIdAndNote" parameterType="map" resultType="role">
        select role_id, role_name, note from t_role
        where role_name like concat(concat('%', #{roleName}), '%')
        and note like concat(concat('%', #{note}), '%')
    </select>

总结

运行成功啦!以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家!

原文链接:https://www.cnblogs.com/dqcer/p/9231269.html

延伸 · 阅读

精彩推荐