开发者

Inserting a single sequence value on multiple rows

I'm trying to insert multiple records into a table, but using the same sequence value for every record.

This is similiar to: How can I insert multiple rows 开发者_开发问答into oracle with a sequence value? however the answer given inserts multiple, different sequence numbers, and I want the same sequence number for multiple recs.

create table test1  ( 
  col_a number, 
  col_b number
);
commit;

create sequence test_seq increment by 1 start with 2 minvalue 1 nocycle nocache noorder;
commit;

insert into test1 (col_a, col_b) 
select a.object_id, test_seq.nextval from (
  select object_id from all_objects where rownum < 5
) a;
commit;

The problem with the above is that it retrieves and inserts multiple (different) "test_seq.nextval" values, and I want the same value inserted for every row.

Is this even possible in straight sql without resorting to a trigger (or multiple sql statements)? One of the answers to the related question hinted it may not be, but it wasn't clear to me.

Thanks.

I'm using Oracle 11g if that helps.


use currval instead of nextval.

select test_seq.nextval from dual;

insert into test1 (col_a, col_b)
select a.object_id, test_seq.currval from (
  select object_id from all_objects where rownum < 5
  ) a;

I know of no method to do that without two statements, the first to increment the sequence (and thus make it selectable through currval) and the second to use currval.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜