trigger
在论坛里面搜索“触发器”,
试着写写看,不难
在论坛里面搜索“触发器”,
试着写写看,不难
解决方案 »
- connect by 语句的问题,请大家帮忙看看吧!
- 请问oracle10g中如何正确显示韩国文字
- 存贮过程调用存贮过程有时成功,有时不成功,请问怎么回事呀 分不够在加呀
- 请问如何实现游标的动态拼装?(急~~~~~~~)
- oci的基础问题请教……
- 请问为什么在update表3前,加select表三
- 问一个分布式事务的问题(同时对SQL Server和Oracle操作)
- 怎样把oracle9i的数据库备份并导入到oracle8.1.6里,在线等待!
- 有没有人能提供:在(oracle+win2000 server)下,热备份的详细过程?小弟不慎感激!
- 关于数据库事务特性和它的隔离级别的困惑?
- oracle Job问题
- 一个过程,一个游标的问题!
create or replace trigger LimitSalForInsert
before insert on emp
for each row
declare
dp varchar2(5);--emp.deptno%type;
sal number(7,2);--emp.sal%type;begin
--select sal into oldsal from emp t where t.rowid = :new.rowid;
select deptno,sum(sal) into dp,sal from emp
where deptno= :new.deptno
group by deptno;
if sal>20000 then
--:new.sal := oldsal;
delete from emp t where t.empno = :new.empno;
dbms_output.put_line('Error:the salary of '||dp||' is over 20000 yuan!');
end if;
end LimitSalForInsert;我本想在这个trigger里面判断新插入的行所属的deptno工资总和是否已经超过了20000,如果超过了,我就把新插入的行删掉。可是,我试了,该插入的还是插入进去了。我插入的数据的sal=20001,deptno=20(表中有属于这个deptno的数据).请各位评判指点。另外,对于update操作我还不知道怎么控制。
误号为ORA-04091:表SCOTT.EMP发生了变化,触发器/函数不能读。
ORA-06512:在“SCOTT.LIMITSALFORINSERT“,line 12
ORA-04088:触发器“SCOTT.LIMITSALFORINSERT“,执行过程中出错。
before insert on emp
for each row
declare
sal number(7,2);--emp.sal%type;
begin
select nvl(sum(sal),0) into sal from emp
where deptno= :new.deptno;
if (sal+:new.sal)>20000 then
raise_application_error(-20010,'Error:the salary of '||:new.deptno||' is over 20000 yuan!');
end if;
end LimitSalForInsert;
/
你的代码同样是在select nvl(sum(sal),0) into sal from emp where eptno= :new.deptno;
的时候报错,错误与我上面列出的一样。
SQL> create table emp(deptno varchar2(3), sal number(7,2));表已创建。SQL> create or replace trigger LimitSalForInsert
2 before insert on emp
3 for each row
4 declare
5 sal number(7,2);--emp.sal%type;
6 begin
7 select nvl(sum(sal),0) into sal from emp
8 where deptno= :new.deptno;
9 if (sal+:new.sal)>20000 then
10 raise_application_error(-20010,'Error:the salary of '||:new.deptno||' is over 20000 yuan!');
11 end if;
12 end LimitSalForInsert;
13 /触发器已创建SQL> insert into emp values('1',10000);已创建 1 行。SQL> insert into emp values('2',20001);
insert into emp values('2',20001)
*
ERROR 位于第 1 行:
ORA-20010: Error:the salary of 2 is over 20000 yuan!
ORA-06512: 在"OPER_112.LIMITSALFORINSERT", line 7
ORA-04088: 触发器 'OPER_112.LIMITSALFORINSERT' 执行过程中出错
SQL> insert into emp values('1',10000);已创建 1 行。SQL> insert into emp values('1',1.2);
insert into emp values('1',1.2)
*
ERROR 位于第 1 行:
ORA-20010: Error:the salary of 1 is over 20000 yuan!
ORA-06512: 在"OPER_112.LIMITSALFORINSERT", line 7
ORA-04088: 触发器 'OPER_112.LIMITSALFORINSERT' 执行过程中出错
SQL> select * from emp;DEP SAL
--- ----------
1 10000
1 10000