您已經以塊顯示代碼。但似乎你正在運行什麼你一起顯示爲腳本,最初不知道更新:
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.
似乎罰款。你確定錯誤信息指的是'T1' - 你還沒有嘗試用不同的名字創建觸發器嗎?你可以查詢'user_errors'視圖來查看哪些是錯誤的。如果這不能讓你清楚,請將相關錯誤添加到您的問題中。 –
創建觸發器後,是否有機會丟棄'SalUpdates'? – Plirkee
當我運行我的整個腳本時,觸發器沒有問題,但現在我收到錯誤「遇到符號更新」爲什麼? – Gary