【问题标题】:syntax error at or near $1 and also some exception sql grammar exception$1 或附近的语法错误以及一些异常 sql 语法异常
【发布时间】:2015-01-13 21:39:16
【问题描述】:

我在 $1 或附近收到错误语法错误,还有一些异常 sql 语法异常

"select t1.* from ( select * from employee_additional_attributes  where stwid in :stwIdList )"
            + "as t1 join ( select stwid, max(employee_additional_attribute_id) as employee_additional_attribute_id from employee_additional_attributes"
            + "where stwid in :stwIdList and mis_attribute_id in (:attributeId) and start_time <= date(:date) group by stwid )"
            + "as t2 using ( stwid, employee_additional_attribute_id" )

【问题讨论】:

  • 能否为您的问题添加例外情况
  • 请添加整个例外。
  • 引起:org.postgresql.util.PSQLException: 错误:“$1”或附近的语法错误
  • 引起:org.hibernate.exception.SQLGrammarException:无法提取结果集
  • @RameshKumar,更新您的问题并添加完整的 java 异常堆栈跟踪

标签: sql hibernate postgresql


【解决方案1】:

PostgreSQL 使用$&lt;n&gt; 语法作为参数化查询中的占位符(例如底层连接中的预准备语句)。

您的第一个 ($1) 参数中有语法错误:
(当然还有@Jens 提到的一些空格语法错误)

where stwid in :stwIdList

IN 的正确语法是 &lt;expression&gt; IN (&lt;expression&gt;, ...)。您不能使用单个参数(使用 JDBC)绑定多个值。如果可以绑定数组,则可以使用&lt;expression&gt; = ANY (&lt;array-expression&gt;) 语法。

对于休眠,请参阅related question(注意查询参数周围的括号)。

【讨论】:

  • 谢谢你能在我的查询中写下并发送给我吗@pozs
  • @RameshKumar 如果你使用hibernate,它只需要在参数两边加上括号,在每个IN之后。这是错误的:stwid in :stwIdList;这应该有效:mis_attribute_id in (:attributeId)(两者都来自您的查询)
猜你喜欢
  • 2021-07-22
  • 2019-10-13
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
  • 2011-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多