2017-04-18 95 views
0

我正在嘗試編寫觸發器來填充包含員工更新薪水信息的表。我現在有一個問題,我現在無法完全包裹我的頭。PL/SQL觸發器問題

這裏是要填充表:

drop table SalUpdates cascade constraints; 
create table SalUpdates(
SalSSN char(9), 
newSalary decimal(10,2), 
oldSalary decimal(10,2) 

); 

這是我的觸發器:

create or replace trigger t1 
after update of salary on employee 
for each row 
begin 
insert into SalUpdates values (:old.Ssn, :new.salary, :old.salary); 
end; 

觸發沒有問題編譯,但是當我嘗試運行此更新,甲骨文告訴我我的觸發器無效。什麼可能導致這個?

update employee 
set salary=4000 
where ssn='123456789'; 
+0

似乎罰款。你確定錯誤信息指的是'T1' - 你還沒有嘗試用不同的名字創建觸發器嗎?你可以查詢'user_errors'視圖來查看哪些是錯誤的。如果這不能讓你清楚,請將相關錯誤添加到您的問題中。 –

+0

創建觸發器後,是否有機會丟棄'SalUpdates'? – Plirkee

+0

當我運行我的整個腳本時,觸發器沒有問題,但現在我收到錯誤「遇到符號更新」爲什麼? – Gary

回答

5

您已經以塊顯示代碼。但似乎你正在運行什麼你一起顯示爲腳本,最初不知道更新:

drop table SalUpdates cascade constraints; 
create table SalUpdates(
SalSSN char(9), 
newSalary decimal(10,2), 
oldSalary decimal(10,2) 
); 

create or replace trigger t1 
after update of salary on employee 
for each row 
begin 
insert into SalUpdates values (:old.Ssn, :new.salary, :old.salary); 
end; 

當作爲一個腳本在SQL Developer中運行該腳本輸出窗口顯示:

drop table SalUpdates cascade constraints 
Error report - 
ORA-00942: table or view does not exist 
00942. 00000 - "table or view does not exist" 
*Cause:  
*Action: 

Table SALUPDATES created. 


Trigger T1 compiled 

如果再加入update語句的腳本:

drop table SalUpdates cascade constraints; 
create table SalUpdates(
SalSSN char(9), 
newSalary decimal(10,2), 
oldSalary decimal(10,2) 
); 

create or replace trigger t1 
after update of salary on employee 
for each row 
begin 
insert into SalUpdates values (:old.Ssn, :new.salary, :old.salary); 
end; 

update employee 
set salary=4000 
where ssn='123456789'; 

你:

Table SALUPDATES dropped. 


Table SALUPDATES created. 


Trigger T1 compiled 

Errors: check compiler log 

如果您然後嘗試運行它自己的更新(作爲語句而不是腳本;或者通過選擇測試並運行一個腳本),你確實得到:

SQL Error: ORA-04098: trigger 'MYSCHEMA.T1' is invalid and failed re-validation 
04098. 00000 - "trigger '%s.%s' is invalid and failed re-validation" 
*Cause: A trigger was attempted to be retrieved for execution and was 
      found to be invalid. This also means that compilation/authorization 
      failed for the trigger. 
*Action: Options are to resolve the compilation/authorization errors, 
      disable the trigger, or drop the trigger. 

如果查詢user_errors視圖,或運行show errors,你會看到:

PLS-00103: Encountered the symbol "UPDATE" 

的問題是,您沒有正確完成create trigger聲明。 update被視爲同一PL/SQL塊的一部分;在無效部分,但仍包括在內。

當你有一個PL/SQL塊必須用斜槓來終止它,as it explains in the SQL*Plus documentation(這主要是適用於SQL開發人員太):

的SQL * Plus把PL/SQL子程序以同樣的方式作爲SQL命令,除了分號(;)或空行不會終止並執行塊。通過在新行上輸入句點(。)自行終止PL/SQL子程序。您也可以通過在新行上自行輸入斜線(/)來終止和執行PL/SQL子程序。

如果添加斜線到PL/SQL代碼和下面的SQL語句所有作品之間你的腳本:

drop table SalUpdates cascade constraints; 
create table SalUpdates(
SalSSN char(9), 
newSalary decimal(10,2), 
oldSalary decimal(10,2) 
); 

create or replace trigger t1 
after update of salary on employee 
for each row 
begin 
insert into SalUpdates values (:old.Ssn, :new.salary, :old.salary); 
end; 
/

update employee 
set salary=4000 
where ssn='123456789'; 

,你現在看到的:

Table SALUPDATES dropped. 


Table SALUPDATES created. 


Trigger T1 compiled 


1 row updated. 
+0

非常感謝你!這是非常豐富的信息,並回答了我的每一個問題。對此,我真的非常感激 – Gary