Postgres:语法错误位于或“附近”为

时间:2016-10-05 16:21:51

标签: java sql postgresql syntax-error jooq

我正在使用JOOQ for java来postgresql数据库连接。它正在尝试运行此查询。

insert into "public"."mission_batches" as "MB" 
  ("micro_task_id", "is_active", "created_at", "updated_at") 
values 
  ('7e1cc9e8-fc11-409b-865e-3bf08e6ca924', false, timestamp '2016-10-05 21:47:13.061', timestamp '2016-10-05 21:47:13.061') returning "MB"."id", "MB"."micro_task_id", "MB"."is_active", "MB"."created_at", "MB"."updated_at"

但是我收到了来自DB的错误

org.jooq.exception.DataAccessException: SQL [insert into "public"."mission_batches" as "MB" ("micro_task_id", "is_active", "created_at", "updated_at") values (?, ?, cast(? as timestamp), cast(? as timestamp)) returning "MB"."id", "MB"."micro_task_id", "MB"."is_active", "MB"."created_at", "MB"."updated_at"];
ERROR: syntax error at or near "as"
  Position: 40

它正在使用我的本地数据库[9.5]。在测试服务器[9.4]上,它抛出了这个错误。我应该在哪里寻找解决方案? Java端还是PG端?

1 个答案:

答案 0 :(得分:3)

您插入的表的别名仅在Postgres 9.5中添加(比较Postgres 9.5's documentation9.4's documentation。由于returning子句中的列无论如何都是指插入的表,没有它就可以轻松做到:

insert into "public"."mission_batches"
  ("micro_task_id", "is_active", "created_at", "updated_at") 
values 
  ('7e1cc9e8-fc11-409b-865e-3bf08e6ca924', false,
   timestamp '2016-10-05 21:47:13.061', timestamp '2016-10-05 21:47:13.061') 
returning 
  "id", "micro_task_id", "is_active", "created_at", "updated_at"