使用带引号的表标识符时,Informix 中的 CONNECT BY 查询引发的一般语法错误
-
21-12-2019 - |
题
在 Informix 数据库上运行以下查询时,数据库报告一般语法错误(没有任何关于导致问题的原因的指示)。相同的查询可以在 CUBRID 或 Oracle 数据库上完美运行,这两种数据库也都支持 CONNECT BY
句法:
select
lower(connect_by_root "t_directory"."name"),
connect_by_isleaf,
connect_by_iscycle,
substr(
sys_connect_by_path(lower("t_directory"."name"), '/'),
2) "dir"
from "t_directory"
start with "t_directory"."parent_id" is null
connect by nocycle prior "t_directory"."id" = "t_directory"."parent_id"
order siblings by lower("t_directory"."name") asc
我使用的数据库是 Windows 上的 Informix 12.10 开发人员版本。我使用以下连接 URL 从 JDBC 驱动程序运行查询(以允许带引号的表标识符):
jdbc:informix-sqli://localhost:9092/test:INFORMIXSERVER=ol_informix;DELIMIDENT=y
解决方案
这里确切的问题是 prior
不接受带引号的表标识符,尽管带引号的列标识符似乎没问题。这个查询运行得很好:
select
lower(connect_by_root "t_directory"."name"),
connect_by_isleaf,
connect_by_iscycle,
substr(
sys_connect_by_path(lower("t_directory"."name"), '/'),
2) "dir"
from "t_directory"
start with "t_directory"."parent_id" is null
connect by nocycle prior t_directory."id" = "t_directory"."parent_id"
order siblings by lower("t_directory"."name") asc
...区别在于:
-- Bad:
connect by nocycle prior "t_directory"."id" = "t_directory"."parent_id"
-- Good:
connect by nocycle prior t_directory."id" = "t_directory"."parent_id"
不隶属于 StackOverflow