我想寫一個函數,將隨機的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
但我仍與環路的一個問題:
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;
當我編譯這個我得到的錯誤:
Error(11,30): PLS-00375: illegal GOTO statement; this GOTO cannot branch to label 'NEXT_UUID'
你爲什麼不故意做一個故事並閱讀錯誤信息? – 2014-10-16 12:08:28
@DanBracuk UUID首次碰撞需要一些時間。 – ceving 2014-10-16 12:15:41