栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PL / SQL触发器问题

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 t1after update of salary on employeefor each rowbegininsert into SalUpdates values (:old.Ssn, :new.salary, :old.salary);  end;

在SQL Developer中作为脚本运行时,脚本输出窗口显示:

drop table SalUpdates cascade constraintsError report -ORA-00942: table or view does not exist00942. 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 t1after update of salary on employeefor each rowbegininsert into SalUpdates values (:old.Ssn, :new.salary, :old.salary);  end;update employeeset salary=4000where ssn='123456789';

你得到:

Table SALUPDATES dropped.Table SALUPDATES created.Trigger T1 compiledErrors: check compiler log

如果然后尝试独自运行更新(作为语句而不是脚本;或者通过选择该测试并作为脚本运行),则确实可以得到:

SQL Error: ORA-04098: trigger 'MYSCHEMA.T1' is invalid and failed re-validation04098. 00000 -  "trigger '%s.%s' is invalid and failed re-validation"*Cause:    A trigger was attempted to be retrieved for execution and wasfound to be invalid.  This also means that compilation/authorizationfailed 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 t1after update of salary on employeefor each rowbegininsert into SalUpdates values (:old.Ssn, :new.salary, :old.salary);  end;/update employeeset salary=4000where ssn='123456789';

您现在看到:

Table SALUPDATES dropped.Table SALUPDATES created.Trigger T1 compiled1 row updated.


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373905.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号