简体   繁体   English

从 DB2 仓库中的列组织表中获取生成的密钥

[英]Get generated key from column organized table in DB2 Warehouse

I am not able to get generated key from column organized table from an INSERT SQL statement run against IBM DB2 warehouse.我无法从针对 IBM DB2 仓库运行的 INSERT SQL 语句中的列组织表中获取生成的密钥。 I am using Java and JDBC driver.我正在使用 Java 和 JDBC 驱动程序。 Everything works fine - I am able to connect to DB, create tables, insert data, I am just not able to get a generated key if it is generated in column organized table.一切正常——我能够连接到数据库、创建表、插入数据,如果它是在列组织表中生成的,我只是无法获得生成的密钥。 Note that row organized tables work fine and return the key properly.请注意,按行组织的表可以正常工作并正确返回键。

Consider a table:考虑一张表:

CREATE TABLE users (
   id       INTEGER not null GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
   username VARCHAR(16),
   PRIMARY KEY (id)
);

If this is row organized table I am able to get the generated key fine by using:如果这是按行组织的表,我可以使用以下方法获得生成的键:

PreparedStatement pr = connection.prepareStatement("INSERT INTO users(username) VALUES(?)", PreparedStatement.RETURN_GENERATED_KEYS);

However, If this is column organized table the PreparedStatemnt creation fails with an error:但是,如果这是按列组织的表,则 PreparedStatemnt 创建会失败并出现错误:

com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-1667, SQLSTATE=42858, SQLERRMC=BLUADMIN.USERS;ORGANIZE BY COLUMN;FINAL|NEW|OLD TABLE, DRIVER=4.25.13

Even if I specify columns I want to get returned like so:即使我指定了列,我也想像这样返回:

PreparedStatement pr = connection.prepareStatement("INSERT INTO users(username) VALUES(?)", new String[]{"id","username"});

pr.setString(1, "test");
pr.executeUpdate();   

I get我得到

com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-1667, SQLSTATE=42858, SQLERRMC=BLUADMIN.USERS;ORGANIZE BY COLUMN;FINAL|NEW|OLD TABLE, DRIVER=4.25.13

on line pr.executeUpdate();在线pr.executeUpdate(); . .

Does this mean that it is not possible to get generated key from column organized table from the INSERT statement in DB2 Warehouse?这是否意味着无法从 DB2 仓库中的 INSERT 语句中获取列组织表的生成键?

Have you tried actually selecting the generated ID?您是否尝试过实际选择生成的 ID? Try something like this:尝试这样的事情:

SELECT ID FROM FINAL TABLE
    (INSERT INTO users(username) VALUES(?))

See " Retrieval of result sets from an SQL data change statement " in the IBM Db2 documentation.请参阅 IBM Db2 文档中的“ 从 SQL 数据更改语句中检索结果集”。

Currently shipping versions v11.1.x and V11.5.x will throw SQL1667N when the query sent to Db2 uses 'FINAL TABLE' or 'OLD TABLE', or 'NEW TABLE' clauses for a column organized table.当发送到 Db2 的查询对列组织表使用“FINAL TABLE”或“OLD TABLE”或“NEW TABLE”子句时,当前发布的版本 v11.1.x 和 V11.5.x 将抛出 SQL1667N。

When you use the jdbc syntax PreparedStatement.RETURN_GENERATED_KEYS , this syntax may be used under the covers.当您使用 jdbc 语法PreparedStatement.RETURN_GENERATED_KEYS时,可能会在幕后使用此语法。

Currently those clauses are not supported (ie will cause the exception to be thrown) for ORGANIZE BY COLUMN tables.目前,ORGANIZE BY COLUMN 表不支持这些子句(即会导致抛出异常)。 There are other restrictions on column organized tables that you should be aware of before using them.在使用按列组织的表之前,您应该了解其他一些限制。

You can workaround this by creating your tables explicitly with the ORGANIZE BY ROW clause.您可以通过使用ORGANIZE BY ROW子句显式创建表来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM