我尝试使用JDBCtemplate执行一个SQL请求(包含XMLQUERY语句)。但是每次执行请求时,DB2都会向我发送一条错误消息,告诉我“错误的SQL语法”。而且关于JDBC的Xquery的文档并不多。
此请求旨在处理我的DB2数据库(包含XML)的列。重要的是要补充一点,我在一家大公司工作,所以我无法下载我想要的所有依赖项,并且以同样的方式,我无法摆脱可以使用的技术。 我尝试在不使用XMLQUERY的情况下执行请求,该请求成功了,所以问题可能出在Xquery和JDBC之间的通信上。 +当我在DB2 DB上直接在Squirel中执行她时,将执行全部请求工作。
1:SQL请求:
String sql = "select XMLQUERY (\n" +
"'for $l in $t/Table/Ligne\n" +
"let $a0 :=$l/@CD_TYP_REJ_REJET\n" +
"where ($a0 != \"AS\")return $l'\n" +
"passing val_cttab as \"t\")\n" +
"from tptcttab\n" +
"where cd_tab_atp_cttab = 'ACTREJ';\n";
2:依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc4</artifactId>
<version>4.24.92</version>
</dependency>
</dependencies>
我希望简单地显示XML格式,但是出现此错误->
1:简短日志:
Wed Sep 11 10:13:17 CEST 2019
There was an unexpected error (type=Internal Server Error, status=500).
PreparedStatementCallback; bad SQL grammar [select XMLQUERY ( 'for $l in $t/Table/Ligne let $a0 :=$l/@CD_TYP_REJ_REJET where ($a0 != "AS")return $l' passing val_cttab as "t") from tptcttab where cd_tab_atp_cttab = 'ACTREJ'; ]; nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;atp_cttab = 'ACTREJ';END-OF-STATEMENT, DRIVER=4.19.49
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [select XMLQUERY ( 'for $l in $t/Table/Ligne let $a0 :=$l/@CD_TYP_REJ_REJET where ($a0 != "AS")return $l' passing val_cttab as "t") from tptcttab where cd_tab_atp_cttab = 'ACTREJ'; ]; nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=;;atp_cttab = 'ACTREJ';END-OF-STATEMENT, DRIVER=4.19.49
所以在这里,我不知道为什么它不适用于JDBC,因此,如果您有一个主意,它可以为我提供很多帮助!
答案 0 :(得分:0)
-104是
SQL0104N An unexpected token "<token>" was found following "<text>".
Expected tokens may include: "<token-list>".
它正在抱怨'ACTREJ';
,所以我认为这是您声明中不需要的;
。
当然这可以从db2 clp和DataStudio中为我运行
select XMLQUERY ('for $l in $t/Table/Ligne let $a0 :=$l/@CD_TYP_REJ_REJET where ($a0 != "AS")return $l' passing val_cttab as "t") from tptcttab where cd_tab_atp_cttab = 'ACTREJ'
尽管我的样品数据没有任何价值