java.sql.SQLException: ORA-00933: SQL 命令在使用 getGeneratedKeys 时未正确结束
java.sql.SQLException: ORA-00933: SQL command not properly ended when using getGeneratedKeys
当我使用以下查询时它有效。
query = "INSERT INTO MLRL1_PSR_MASTER (PROJECT_ID,FROM_DATE,TO_DATE,TEMPLATE_ID,TEMPLATE_TYPE,UPLOADED_BY,PSR_SLABID) " +
" select '"+projectId+"' , FROM_DATE , TO_DATE,'"+templateId+"','"+tempType+"','"+user.getUserID()+"', "+slabId+
" from MLRL1_PSR_SLABS where SLAB_ID="+slabId+" ";
stmt = connection.prepareStatement(query, new String[] { "ID" });
stmt.executeUpdate();
stmt = connection.prepareStatement(query);
但如果我对 getGeneratedKeys() 使用相同的查询,例如:
stmt = connection.prepareStatement(query, new String[] { "ID" });
stmt.executeUpdate();
ResultSet rs = stmt.getGeneratedKeys();
while (rs.next()) {
masterId = rs.getInt(1);
}
我收到一个错误
ORA-00933: SQL command not properly ended
stmt
为 java.sql.PreparedStatement
,代码合规性为 1.6,JRE 为 1.7.67 Oracle 驱动程序为 odbc6,数据库为 Oracle 数据库 11g 企业版 Release 11.2.0.1.0
我认为语法不正确。您可能需要在查询中添加 'VALUES'。插入查询应该类似于 - INSERT INTO TABLE_NAME VALUES ();
ResultSet rs = stmt.getGeneratedKeys();
JDBC 驱动程序会将 'RETURNING . . . INTO . . ' 添加到您提供的查询的末尾,以便 return 要求的值。
在您的情况下,指定总是为 INSERT 生成的 SLAB_ID,因为您没有指定自己的列。
当 'RETURNING ..' 添加到查询的末尾时,生成的语法无效并给出错误消息。
ORA-00933: SQL command not properly ended
返回。 . .仅支持 INSERT...VALUES;也就是说,对于使用 VALUES 子句提供要插入的值的 INSERT 语句。使用子查询的插入不支持该语法。
请参阅 Oracle SQL 参考中 INSERT 语句的语法图 https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9014.htm#SQLRF01604
请注意,'returning_clause' 仅显示在 'values_clause' 行中,而不显示在 'subquery' 行中。
不支持您尝试使用的语法。
您可以阅读更多内容:
http://database.developer-works.com/article/16369238/INSER+INTO+with+SELECT+is+throwing+error
当我使用以下查询时它有效。
query = "INSERT INTO MLRL1_PSR_MASTER (PROJECT_ID,FROM_DATE,TO_DATE,TEMPLATE_ID,TEMPLATE_TYPE,UPLOADED_BY,PSR_SLABID) " +
" select '"+projectId+"' , FROM_DATE , TO_DATE,'"+templateId+"','"+tempType+"','"+user.getUserID()+"', "+slabId+
" from MLRL1_PSR_SLABS where SLAB_ID="+slabId+" ";
stmt = connection.prepareStatement(query, new String[] { "ID" });
stmt.executeUpdate();
stmt = connection.prepareStatement(query);
但如果我对 getGeneratedKeys() 使用相同的查询,例如:
stmt = connection.prepareStatement(query, new String[] { "ID" });
stmt.executeUpdate();
ResultSet rs = stmt.getGeneratedKeys();
while (rs.next()) {
masterId = rs.getInt(1);
}
我收到一个错误
ORA-00933: SQL command not properly ended
stmt
为 java.sql.PreparedStatement
,代码合规性为 1.6,JRE 为 1.7.67 Oracle 驱动程序为 odbc6,数据库为 Oracle 数据库 11g 企业版 Release 11.2.0.1.0
我认为语法不正确。您可能需要在查询中添加 'VALUES'。插入查询应该类似于 - INSERT INTO TABLE_NAME VALUES ();
ResultSet rs = stmt.getGeneratedKeys();
JDBC 驱动程序会将 'RETURNING . . . INTO . . ' 添加到您提供的查询的末尾,以便 return 要求的值。 在您的情况下,指定总是为 INSERT 生成的 SLAB_ID,因为您没有指定自己的列。 当 'RETURNING ..' 添加到查询的末尾时,生成的语法无效并给出错误消息。
ORA-00933: SQL command not properly ended
返回。 . .仅支持 INSERT...VALUES;也就是说,对于使用 VALUES 子句提供要插入的值的 INSERT 语句。使用子查询的插入不支持该语法。
请参阅 Oracle SQL 参考中 INSERT 语句的语法图 https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9014.htm#SQLRF01604
请注意,'returning_clause' 仅显示在 'values_clause' 行中,而不显示在 'subquery' 行中。
不支持您尝试使用的语法。
您可以阅读更多内容:
http://database.developer-works.com/article/16369238/INSER+INTO+with+SELECT+is+throwing+error