【问题标题】:Syntax error in SQL statement - H2 error 42001SQL 语句中的语法错误 - H2 错误 42001
【发布时间】:2015-06-19 01:39:18
【问题描述】:

运行此 SQL 语句时:

select TimeInterval, 
       ((((Timer*60)/1.0)*100)/((10.0*60)/60.0)) as 'Throughput-run_1_8_11' 
from StatExternalData, StatisticDefinition 
where StatisticDefinition.ID=StatExternalData.StatDefId 
      and StatisticName='PSI_CompTran_Successful_Cnt'  
order by TimeInterval asc

我收到此错误:

"select TimeInterval, ((((Timer*60)/1.0)*100)/((10.0*60)/60.0)) as 'Throughput-run_1_8_11'[*] from StatExternalData, StatisticDefinition where StatisticDefinition.ID=StatExternalData.StatDefId and StatisticName='PSI_CompTran_Successful_Cnt'  order by TimeInterval asc"; 
expected "identifier"; [42001-185]

我发现 [*] 表示语句的哪一部分不正确,而 H2 错误代码 42001 表示 SQL 语句无效,但我数周来一直在努力想弄清楚出了什么问题,有人有想法吗?

【问题讨论】:

  • 您是否尝试用双引号 " " 而不是单引号 (' ') 将列别名括起来?
  • 另外,尽量避免使用破折号-。尝试 Throughput_run_1_8_11 而不是 Throughput-run_1_8_11
  • as 'Throughput-run_1_8_11' 是无效的 SQL。单引号用于字符串文字。双引号用于标识符
  • 感谢 barranka abd a_horse_with_no_name,这就是问题所在!

标签: sql h2


【解决方案1】:

我遇到了同样的问题:

我的实体看起来像这样:

@Entity
public class ShopCommentRating {

@NotNull
private Boolean like;

}

生成的查询包含一个 [*]

为了消除错误,我不得不将字段名称更改为某事。像这样:

@Entity
public class ShopCommentRating {

@NotNull
private Boolean commentLike;

}

'小写驼峰'名称

【讨论】:

  • 在您的情况下,“like”是保留的 SQL 字。任何其他名称都可以。
  • 如果你真的必须使用保留关键字作为列名(我不建议这样做),你也可以escape列名。
【解决方案2】:

如果是 42001-197,您还可以检查 Oracle 分析函数和 H2 版本,它需要从 1.4.198 开始,尤其是 ROW_NUMBER OVER PARTITION。

【讨论】:

    【解决方案3】:

    查看 H2 中的 SQL Grammar page。在我的情况下,问题是表名'order'和破折号('-'),而不是下划线('_')。愚蠢但致命。

    在您的查询中,别名 'Throughput-run_1_8_11' 的破折号会有问题。

    【讨论】:

      猜你喜欢
      • 2023-01-12
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多