2013-03-10 126 views
0

oracle序列旨在返回自動遞增的數字。我想知道我是否可以實現一個自定義的序列,該序列從我放入的值池中返回,以便我可以告訴序列預期返回的結果。如何實現自定義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; 
+0

它可能取決於你想得到什麼,你能舉個例子嗎? – 2013-03-10 14:28:07

+0

例如,我想開發一個自定義的序列,它返回範圍[2,7],[200,10000],[10001,300000],[99999,9998888]等等......所以我想在這裏從特定範圍獲得自動增加。 – 2013-03-10 14:39:06

回答

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; 
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,...

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;