2014-10-16 1 views
0

임의의 UUID를 테이블에 삽입하는 함수를 작성하고 싶습니다. 이 함수는 UUID를 성공적으로 삽입 한 후 UUID를 반환해야합니다. 기본 키 충돌의 경우 함수가 성공할 때까지 다른 UUID를 시도하려고합니다.기본 키가 위반을 제한한다는 예외의 이름은 무엇입니까?

나는 지금까지 무엇을 가지고 :

create or replace 
function new_object_id return raw is 
    v_oid RAW(16); 
begin 
<<next_uuid>> 
    v_oid := random_uuid(); 
    insert into object (object_id) values (v_oid); 
    commit; 
    exception 
    when ?primary_key_constrain_failure? then goto next_uuid 
    end; 
    return v_oid; 
end new_object_id; 

하지만 기본 키 제약을 위반 때 발생하는 예외에 대한 올바른 이름을 알아낼 수 없습니다. 아무도 몰라?

업데이트

나는 dup_val_on_index을 시도하지만, 난 여전히 루프에 문제가 있습니다 this에 따르면

Error(11,30): PLS-00375: illegal GOTO statement; this GOTO cannot branch to label 'NEXT_UUID' 
+1

왜 실수로 하나를 수행하고 오류 메시지를 읽지 않습니까? –

+0

@DanBracuk UUID가 첫 번째 충돌을 일으킬 때까지 약간의 시간이 걸립니다. – ceving

답변

2

:이를 컴파일 할 때

create or replace 
function new_object_id return raw is 
    v_oid RAW(16); 
begin 
<<next_uuid>> 
    v_oid := random_uuid(); 
    insert into object (object_id) values (v_oid); 
    commit; 
    return (v_oid); 
exception 
    when DUP_VAL_ON_INDEX then goto next_uuid; 
end new_object_id; 

내가 오류를 DUP_VAL_ON_INDEX입니다.

전체의 동작 시험 :

create table x 
(y number(15,0) 
, constraint x_pk primary key (y) 
) 
; 

begin 
    insert into x (y) values (1); 
exception 
    when dup_val_on_index 
    then 
    dbms_output.put_line('ARGH!!!'); 
end; 

는 2 부 들어, 캡슐화 begin ... end 블록 사용

begin 
    <<test>> 
    begin 
    insert into x values (1); 
    exception 
     when dup_val_on_index then goto test; -- I know, a loop, but it is for the demo 
    end; 
end; 
+0

고맙지 만 루프는 어때? – ceving

+0

[ "GOTO 문은 예외 처리기로 분기 할 수 없으며 GOTO 문을 예외 처리기에서 현재 블록으로 분기 할 수 없습니다."] (http://docs.oracle.com/cd/B10500_01/appdev.920/ a96624/07_errs.htm) –

+0

@ceving : 업데이트 된 답변을 참조하십시오. –

0

는 이제 컴파일 :

create or replace 
function new_object_id return raw is 
    v_oid RAW(16); 
begin 
<<next_uuid>> 
    begin 
    v_oid := random_uuid(); 
    insert into object (object_id) values (v_oid); 
    commit; 
    return (v_oid); 
    exception 
    when dup_val_on_index then goto next_uuid; 
    end; 
end new_object_id; 
0

는 루프 시도를 사용하여이 작업을 수행하려면 :

create or replace function new_object_id return raw is 
    v_oid RAW(16); 
begin 
    LOOP 
    begin 
     v_oid := random_uuid(); 
     insert into object (object_id) values (v_oid); 
     EXIT; 
    exception 
     when dup_val_on_index then 
     NULL; -- do nothing, roll around to top of LOOP again 
    end; 
    END LOOP; 

    commit; 

    return (v_oid); 
end new_object_id; 

공유하고 즐기십시오.

관련 문제