我尝试使用触发器在我插入的新值未遵循先前值时显示RAISE_APPLICATION_ERROR。例如,我的最新位置在第二位置,当我想插入新位置10时,它应显示ORA-…“我输入的错误消息”?任何人都可以帮助我编辑我的代码?我需要一个选择语句吗?
SET ECHO ON
SET FEEDBACK ON
SET serveroutput on size 2000
CREATE OR REPLACE TRIGGER RowTrigger
Before
INSERT OR UPDATE on POSITION
FOR EACH ROW
declare
u number(2);
begin
select count(P#) into u from POSITION ;
if :new.P# < u
then
RAISE_APPLICATION_ERROR(-20011,'dddd');
end if;
end;
答案 0 :(得分:0)
好的。有时候,我花了一些时间来编写它,下面的工作考虑了这个问题:
-The P# is unique, or youll get error in the select.
-I am not taking on consideration the previous data , for example if your P# is 5 and ur inserting 3, then youll not have error.
if you need such thing you can continue developing it because there are some conditions to be met.
-The table already contains data , all you can add a exception NO_data_found and do whatever you want in that case.
这是代码:
drop table ex_employee
/
create table ex_employee (id number(2) null,id1 number(2) NULL,name varchar2(1000) null)
/
drop table POSITION
/
create table POSITION(p# number(2) null)
/
insert into POSITION(p#) values (4)
/
insert into POSITION(p#) values (5)
/
commit
/
CREATE OR REPLACE TRIGGER RowTrigger
FOR INSERT on POSITION compound trigger
prevID number(4);
new_p number(4);
after each row is
begin
new_p := :new.p#;
end after each row;
after statement is
begin
--it gives the previous record;
select PREV_ID into prevID from(SELECt p#, LAG(p#, 1, 0) OVER(ORDER BY p#) as PREV_ID FROM position) where p# = new_p;
-- check if the old data is smaller then the new, you can add more conditions.
if (previd < new_p) then
previd:=previd+1;
end if;
IF (prevID != new_p) THEN
RAISE_APPLICATION_ERROR(-20011, 'ERROR , data not in order or whatever.');
END IF;
end after statement;
end;
/