不,只有在不存在時才插入。這需要兩個操作。你必須檢查它是否存在,然後你必須插入記錄。
正確的方法是在桌面上創建unique constraint。
ALTER TABLE table_name
add CONSTRAINT constraint_name UNIQUE (city);
你再搭上插入一個已經存在的城市時引發的異常,然後做任何你:如文檔中,或者如果你的表已經存在,你可以改變它添加約束規定你可以做到這一點在線希望獲得的信息。
您也錯誤地增加了您的ID。您應該使用SEQUENCE,這可以爲您節省另一個SELECT。
CREATE SEQUENCE city_seq
START WITH <current max ID>
INCREMENT BY 1;
你的過程就變成了:
create or replace procedure PlaceName (
town in city.name%type) is
begin
insert into city
values(city_seq.nextval, town);
-- Catch the raised exception if the city already exists.
exception when dup_val_on_index then
<do something>;
end;
來源
2013-01-20 22:45:32
Ben