Хорошо.Мне иногда приходилось писать это, ниже работает с учетом:
-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;
/