我们假设
Table TARGET {
ID IDENTITY ...,
...
JOB_NO NUMBER
}
我的问题是,如何生成JOB_NO(通过序列),以便在语句中提交的所有插入具有相同的job_no。
输出应该类似于插入stmt的第n次执行。
ID ... JOB_NO
1 ... 123
2 ... 123
3 ... 123
以下显然不起作用。但是正确的Oracle语法是什么?
INSERT INTO TARGET SELECT A.*, B.* FROM my_source_table A, (SELECT x.nextval from dual) B
非常感谢 尔根
答案 0 :(得分:1)
您可以为每个此类插入选择NEXTVAL
,这会增加sequence
,然后在CURRVAL
中使用INSERT
。
让我们说这是你的序列
create sequence seq START WITH 123 ;
始终在INSERT
中指定列名以避免混淆,请勿使用select * from
select seq.NEXTVAL FROM DUAL;
INSERT INTO TARGET(JOB_NO,col1,col2,..)
SELECT seq.CURRVAL, a.col1,a.col2 FROM my_source_table A, ..
答案 1 :(得分:0)
您应该可以执行以下操作:
insert into target select my_seq.nextval, a.* from source a;
根据以下评论进行更新:
SQL*Plus: Release 12.1.0.2.0 Production on Wed Apr 11 12:48:21 2018
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 12c Standard Edition Release 12.1.0.2.0 - 64bit Production
With the Automatic Storage Management option
SQL> create table a(id number, name varchar2(10));
Table created.
SQL> insert into a values(1,'Larry');
1 row created.
SQL> c/1/2
1* insert into a values(2,'Larry')
SQL> c/Larry/Moe
1* insert into a values(2,'Moe')
SQL> /
1 row created.
SQL> c/2/3
1* insert into a values(3,'Moe')
SQL> c/Moe/Curly
1* insert into a values(3,'Curly')
SQL> /
1 row created.
SQL> commit;
Commit complete.
SQL> select * from a;
ID NAME
---------- ----------
1 Larry
2 Moe
3 Curly
SQL> create table b as select 1 id, a.id id_a, a.name from a where 1=0;
Table created.
SQL> desc b
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER
ID_A NUMBER
NAME VARCHAR2(10)
SQL> create sequence seq_a;
Sequence created.
SQL> insert into b select seq_a.nextval, a.* from a;
3 rows created.
SQL> select * from b;
ID ID_A NAME
---------- ---------- ----------
1 1 Larry
2 2 Moe
3 3 Curly
希望有所帮助。