2017-04-18 93 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