如何实现自定义oracle序列?

时间:2013-03-10 14:21:58

标签: database oracle jdbc oracle10g oracle11g

oracle序列旨在返回自动增加的数字。我想知道我可以实现一个从我输入的值池返回的自定义序列,所以我可以告诉序列预期会返回什么。

无论如何要实现这个?或替代,如添加触发器,或oracle函数或其他任何东西?

甚至可以使用表格保存值,但怎么做才是最好的表现,比如oralce sequence

I have this, but not sure is it the best one.
create table your_table(
   gap_from int,
   gap_to int
);

insert into your_table values(99999, 9998888);
insert into your_table values(2, 7);
insert into your_table values(200, 10000);
insert into your_table values(10001, 300000);

create table2 as  select
          gap_from,
          1 + nvl(sum(gap_to - gap_from + 1) over (order by gap_from rows between unbounded preceding and 1 preceding), 0) as seq_from,
          sum(gap_to - gap_from + 1) over (order by gap_from) as seq_to
        from your_table;

create sequence your_new_seq;


create or replace function get_PK_inside_gap return number as
  new_seq_val number;
  PK_inside_gap number;
begin
  select your_new_seq.nextval into new_seq_val from dual;
  execute immediate '
    select
      new_seq_val + gap_from - seq_from
    from
      (select :1 as new_seq_val from dual)
      join (
       table2 
      ) on new_seq_val between seq_from and seq_to'
  into PK_inside_gap using new_seq_val;
  return PK_inside_gap;
end;

4 个答案:

答案 0 :(得分:1)

仅返回范围5到10的值:

 create sequence seq1 start with 5 maxvalue 10;

仅返回100000到999990之间的值,跳转为10:

 create sequence seq2 start with 100000 maxvalue 999990 increment 10;

答案 1 :(得分:0)

创建一个包含所需条目的表,然后将表固定在内存中以获得良好的性能。当值“使用”时,您可以更新它们以设置状态标志。

在来自内存固定表中的查询中,查询可以选择未使用的最小条目。或者,如您所说,您可以根据其他标准选择查询。

答案 2 :(得分:0)

您可以根据差距列表创建常规序列并修改其结果。

create table your_table(
   gap_from int,
   gap_to int
);

insert into your_table values(99999, 9998888);
insert into your_table values(2, 7);
insert into your_table values(200, 10000);
insert into your_table values(10001, 300000);

create sequence your_new_seq;


create or replace function get_PK_inside_gap return number as
  new_seq_val number;
  PK_inside_gap number;
begin
  select your_new_seq.nextval into new_seq_val from dual;
  execute immediate '
    select
      new_seq_val + gap_from - seq_from
    from
      (select :1 as new_seq_val from dual)
      join (
        select
          gap_from,
          1 + nvl(sum(gap_to - gap_from + 1) over (order by gap_from rows between unbounded preceding and 1 preceding), 0) as seq_from,
          sum(gap_to - gap_from + 1) over (order by gap_from) as seq_to
        from your_table
      ) on new_seq_val between seq_from and seq_to'
  into PK_inside_gap using new_seq_val;
  return PK_inside_gap;
end;

现在拨打get_PK_inside_gap以获取未使用的PK序列:2,3,4,5,6,7,200,201,......

答案 3 :(得分:0)

老兄,我认为这可能对你有所帮助,通常一旦你创建了序列,你就不能用参数指定start,所以最好删除该序列并重新创建它;

create or replace
procedure SEQ_alter(start_with in number,end_with in number,seq_name varchar2)
   as
   sql_qury varchar2(148);
   drop_qury varchar2(148);
   begin
   drop_qury:='drop sequence '||seq_name ;
   execute immediate drop_qury;
   sql_qury:='create sequence '||seq_name|| ' start with '|| to_number(start_with ) ||' maxvalue '|| to_number(end_with);

   execute immediate sql_qury;
   end;