简体   繁体   中英

Doing an INSERT ALL using JDBC in ORACLE

I am using Oracle Database 12c Enterprise Edition

I want to insert records into 2 tables say TABLE1 and TABLE2 back to back using JDBC. These 2 tables have a primary key and foreign key relationship based on a common column say ID_COLUMN

I am planing to use the following single query and fire it via my Java application via JDBC:

insert all 
  into TABLE1 (ID_COLUMN,COL2,COL3,COL4,COL5,COL6) values(?,?,?,?,?,?)
  into TABLE2 (COL1_1,COL_1_2,COL_1_3,ID_COLUMN) values('blah',42,'rubbish', 
   select test_ctrl.seq_test_id.nextval FROM dual) 
  select * from dual;

My basic requirement is that I need to INSERT TABLE2 with the latest ID_COLUMN from TABLE1 from my current session. I know the usage of select test_ctrl.seq_test_id.nextval FROM dual in the INSERT ALL statement is not correct. But it being Oracle I cant use SCOPE_IDENTITY()

Please suggest how can I make this query work

"But it being Oracle I cant use SCOPE_IDENTITY()"

Ah but you can. In Oracle 12c they introduced identity columns: these are a special variant of virtual columns.

create table my_table (
    id number generated always as identity 
    ....
    , constraint my_table_pk primary key (id)

Find out more.

我似乎已经找到了问题的答案。像这样修改查询。请注意edme_ctrl.seq_ts_annotation_id.nextval和edme_ctrl.seq_ts_annotation_id.currval

INSERT ALL INTO "SPI7CG_CgNvI".X$ANNOTATIONS(ANNOTATION_ID,CATEGORY,REASON,COMMENTS,AUTHOR,ADJUSTMENT_TYPE,ADJUSTMENT_VALUE) VALUES (edme_ctrl.seq_ts_annotation_id.nextval, '51','33','Test Bulk Insert','kshiam','A',10) INTO "SPI7CG_CgNvI".X$DATA_ANNOTATIONS(ANNOTATION_ID, TABLE_NAME, TABLE_ROW_ID,COLUMN_NAME) VALUES (edme_ctrl.seq_ts_annotation_id.currval,'W$XXXXXGNVBSNSSNDCTRSSR007',164921155,'IVXXXXXGNVBXWGSQDTWQRTR0003') select * from dual

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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