小编典典

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;

触发器编译没有问题,但是当我尝试运行此更新时,Oracle告诉我触发器无效。是什么原因造成的?

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

阅读 422

收藏
2021-03-08

共1个答案

小编典典

您已按块显示了代码。但似乎您正在运行一起显示为脚本的内容,最初没有进行更新:

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块时,必须使用斜杠将其终止,如在SQL * Plus文档(大多数情况下也适用于SQLDeveloper)中所解释的那样:

SQL * Plus以与SQL命令相同的方式对待PL /
SQL子程序,除了分号(;)或空白行不会终止并执行块之外。通过在新行上单独输入一个句点(。)来终止PL /
SQL子程序。您还可以通过在新行上单独输入一个斜杠(/)来终止并执行PL / SQL子程序。

但是,SQL Developer不会抱怨脚本中的最后一个块是否没有终止斜杠,因此您的原始脚本(不进行更新)可以工作;在SQL * Plus中,它将位于提示符处。从某种意义上讲,它应该在那里-
试图有所帮助。当您添加update语句时,它不再是脚本的结尾,因此不再适用。

如果在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.
2021-03-08