0

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

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

甚至使用一个表来保存值,但如何做到最好是性能,比如 oralce 序列

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

4 回答 4

1

要仅返回 5 到 10 范围内的值:

 create sequence seq1 start with 5 maxvalue 10;

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

 create sequence seq2 start with 100000 maxvalue 999990 increment 10;
于 2013-03-10T16:08:09.387 回答
0

伙计,我认为这可能会对您有所帮助,通常一旦您创建了序列,就无法从参数开始分配,因此最好删除该序列并重新创建它;

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;
于 2013-03-11T07:39:37.923 回答
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,...

于 2013-03-10T16:09:05.660 回答
0

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

在来自内存固定表的查询中,查询可以选择未“使用”的最小条目。或者,您可以根据您提到的其他条件进行查询选择,因为您需要这种灵活性。

于 2013-03-10T16:00:12.697 回答