2016-12-15 63 views
-3

我試圖在oracle apex中創建一個觸發器,它將選擇我在UniversityRefNo列中的所有數據,並且不允許用戶放入已存在的值。但是,我不斷收到此錯誤:"ORA-24344: success with compilation error."如果有人可以告訴我什麼即時做錯了,那將是很大的。ORA-24344:編譯錯誤成功。 Trigger apex

CREATE TABLE VOLUNTEER(
Volunteer_id   NUMBER(5) PRIMARY KEY,  
Title    VARCHAR2(5),  
VolName    VARCHAR2(30), 
UniversityRefNo  VARCHAR2(10),  
Address_Line_1  VARCHAR2(30), 
Address_Line_2  VARCHAR2(12),  
PostCode    VARCHAR2(12),  
Mobile_Number  NUMBER(12),  
Home_Number   NUMBER(12),  
Email    VARCHAR2(25),  
Ethnic_Group   VARCHAR2(10),  
VolunteerType  VARCHAR2(10), 
Register_Disable  VARCHAR2(3), 
Gender    VARCHAR2(1), 
Medical_Details  VARCHAR2(20), 
PassPortNumber  NUMBER(10), 
Nationality   VARCHAR2(10), 
Passport_Expiry_Date DATE,  
Date_of_Birth  DATE, 
Course_Name   VARCHAR2(20), 
Course_Year   VARCHAR2(10), 
Area_of_work   VARCHAR2(15),  
StaffManagerName  VARCHAR2(20),  
StaffManagerEmail VARCHAR2(20),  
StaffManagerPhone VARCHAR2(12)); 

CREATE or replace trigger unirefnoTrigger  
before insert or update of UniversityRefNo ON VOLUNTEER for each row  
begin  
SELECT UniversityRefNo from VOLUNTEER  
if(UniversityRefno = :new.UniversityRefNo) then raise application error(UniversityRefNO || :new.UniversityRefNo || 'already exists'); 
end if;  
end; 

回答

0

如果你想列UniversityRefNo獨特您不需要進行觸發,你可以簡單的創建一個唯一約束是這樣的:

ALTER TABLE VOLUNTEER 
ADD CONSTRAINT UniversityRefNo_UK UNIQUE (UniversityRefNo); 

只是爲了下面學習的目的是如何可以創建一個觸發器來做同樣的事情,但我不建議你使用它。

CREATE or replace trigger unirefnoTrigger 
before insert or update of UniversityRefNo ON VOLUNTEER for each row 
university_count number; 
begin 
SELECT count(*) into university_count from VOLUNTEER where UniversityRefno=:new.UniversityRefNo; 
if university_count<>0 then 
raise application error(-20000, :new.UniversityRefNo || ' already exists'); 
end if; 
end; 
+0

我覺得OP的目的是給終端用戶比一個更具描述性的錯誤消息「ORA-00001:唯一約束(constraint_name命令)違反了」,但是請注意,有更好的方法來做到這一點。 – Typo