2016-12-07 67 views
0

我正在嘗試創建一個觸發器,它將爲血庫提供的捐獻血量並將其添加到該特定血型的當前數量。我可以毫無問題地編譯觸發器。但是,當我運行一些條目時,出現三個錯誤,ORA-04091,ORA-06512和ORA-04088。它表示該表正在發生變化,因此觸發器無法訪問它,但在插入觸發器後使用,所以不應該插入表?任何建議都會很棒,教授並沒有真正涉及觸發器。ORA-04091創建觸發器時出錯

觸發:

create or replace trigger addBloodCount 
after insert on Donations 
for each row 
when(new.donationID is not null AND new.donorID is not null AND new.bloodCode is not null AND new.donationDate is not null AND new.amountDonated   is not null) 
declare 
add_amount INTEGER; 
begin 
select amountDonated into add_amount from Donations; 
update Blood 
set quantity = Blood.quantity + add_amount 
where bloodCode =:new.bloodCode; 
end; 
/

表:

CREATE TABLE Address (
addressID INTEGER NOT NULL, 
street VARCHAR(50) NOT NULL, 
city VARCHAR(40) NOT NULL, 
state VARCHAR(30) NOT NULL, 
zip INTEGER NOT NULL, 
PRIMARY KEY (addressID)); 


CREATE TABLE Donor (
donorID INTEGER NOT NULL, 
fname VARCHAR(50) NOT NULL, 
lname VARCHAR(50) NOT NULL, 
sex VARCHAR(10) NOT NULL, 
addressID INTEGER NOT NULL, 
DOB DATE NOT NULL, 
phoneNo INTEGER NOT NULL, 
PRIMARY KEY (donorID)); 


CREATE TABLE Donations (
donationID INTEGER NOT NULL, 
donorID INTEGER NOT NULL, 
bloodCode INTEGER NOT NULL, 
donationDate DATE NOT NULL, 
amountDonated INTEGER NOT NULL, 
PRIMARY KEY (donationID)); 


CREATE TABLE Blood (
bloodCode INTEGER NOT NULL, 
bloodType VARCHAR(50) NOT NULL, 
bloodPrice DECIMAL NOT NULL, 
quantity INTEGER NOT NULL, 
PRIMARY KEY (bloodCode)); 


CREATE TABLE BloodOrder (
orderID INTEGER NOT NULL, 
bloodCode INTEGER NOT NULL, 
hospitalID INTEGER NOT NULL, 
amountOrdered INTEGER NOT NULL, 
PRIMARY KEY (orderID)); 


CREATE TABLE Hospital (
hospitalID INTEGER NOT NULL, 
hospitalName VARCHAR(50) NOT NULL, 
addressID INTEGER NOT NULL, 
phoneNo INTEGER NOT NULL, 
PRIMARY KEY (hospitalID)); 


alter table Donor 
add (constraint addressID_fk foreign key (addressID) 
references Address(addressID)); 

alter table Donations 
add (constraint donorID_fk foreign key (donorID) 
references Donor(donorID)); 

alter table Donations 
add (constraint bloodCode_fk foreign key (bloodCode) 
references Blood(bloodCode)); 

alter table bloodOrder 
add (constraint bloodCode_fk2 foreign key (bloodCode) 
references Blood(bloodCode)); 

alter table bloodOrder 
add (constraint hospitalID_fk foreign key (hospitalID) 
references Hospital(hospitalID)); 

alter table Hospital 
add (constraint addressID_fk2 foreign key (addressID) 
references Address(addressID)); 

回答

0

問題是由這個說法引起:

select amountDonated into add_amount from Donations; 

總之,你不能引用直接插入值。你需要使用:NEW來代替。重新寫觸發如下應該工作:

create or replace trigger addBloodCount 
after insert on Donations 
for each row 
when(new.donationID is not null AND new.donorID is not null AND new.bloodCode is not null AND new.donationDate is not null AND new.amountDonated is not null) 
begin 
update Blood 
set quantity = quantity + :new.amountDonated 
where bloodCode =:new.bloodCode; 
end; 
/

語句或許不需要

when(new.donationID is not null AND new.donorID is not null AND new.bloodCode is not null AND new.donationDate is not null AND new.amountDonated is not null) 

既然你已經有了所有這些列設置NOT NULL約束。

+0

非常感謝你! – Christian