Oracle
#触发器
关于如何在PL/SQL中代替sql语句中exists语句的用法
疑惑:
在plsql程序中,使用了以下语句,企图在触发器中使用类似于exists的语句,从而实现判断是否更新成功的目标
原始代码如下:
if updating then
update emp set DEPTNO=:new.deptno where EMP.DEPTNO=:old.deptno;
--输出是否插入成功
if exists(select DEPTNO from EMP where EMP.DEPTNO=(select DEPT.DEPTNO from DEPT)) then
DBMS_OUTPUT.PUT_LINE('emp表更新成功');
else
DBMS_OUTPUT.PUT_LINE('抱歉貌似是失败了欸');
end if;
end if;
执行之后报错:
[2021-12-03 23:51:11] 5:12:PLS-00204: 函数或伪列 'EXISTS' 只能在 SQL 语句中使用
[2021-12-03 23:51:11] 5:9:PL/SQL: Statement ignored
解决方法
经过资料查阅,说是要用隐式游标 : sql%found来处理
但是我的这个问题我好像没法用这个解决
后来经过作者的慢慢摸索,尝试出了一下方法,定义一个变量,查询值赋进去不就好了…
......
此处省略一万字
......
--定义变量 v_count用于存放值
declare
v_count number :=0;
......
......
......
begin
if updating then
update emp set DEPTNO=:new.deptno where EMP.DEPTNO=:old.deptno;
--输出是否插入成功
--关键来了!!!
--用select into语句来化解plsql中
--不能使用exists语句的尴尬就ok啦!
select count(*) into v_count from emp
where(exists(select DEPTNO from EMP where EMP.DEPTNO=(select DEPT.DEPTNO from DEPT)));
if v_count<>0 then
DBMS_OUTPUT.PUT_LINE('emp表更新成功');
else
DBMS_OUTPUT.PUT_LINE('抱歉貌似是失败了欸');
end if;
end if;
end;
插入链接与图片
欢迎光临我的主页: link.