| How to restrict the inserting a record through a oracle trigger [message #634293] |
Sat, 07 March 2015 01:59  |
 |
shivas1177
Messages: 5 Registered: March 2015
|
Junior Member |
|
|
Hi All,
I want to restrict the records which are inserting into table a where i am creating trigger on that of before insert.
I have a scenario
Table A
id number
id1 number
Table B
id number
Table c
id number
Table D
id number
id1 number
My code:
create or replace trigger trg_a
before insert on a
for each row
declare
v_id b.id%type;
v_id1 c.id%type;
pragma autonomous_transaction;
begin
select id into v_id from b where id=:NEW.id;
select id into v_id1 from c where id=:NEW.id;
if(:NEW.id=v_id or :NEW.id=v_id1) then
insert into d
(id,id1,name)
values(:NEW.id,:NEW.id1,:NEW.name);
end if;
delete from a where id =v_id or id1=v_id1;
commit;
end;
/
It is inserting for matching records with the look up tables i.e b,c and parallely also inserting into table a.This record insertion need to restrict so i have kept delete stmt but it is not working. Can any one please help.
Thanks in advance
Shiva
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Re: How to restrict the inserting a record through a oracle trigger [message #634303 is a reply to message #634300] |
Sat, 07 March 2015 03:07   |
 |
sss111ind
Messages: 636 Registered: April 2012 Location: India
|
Senior Member |

|
|
SET DEFINE OFF;
CREATE OR REPLACE TRIGGER emp_trig
BEFORE INSERT ON emp
FOR EACH ROW
DECLARE
pragma autonomous_transaction;
v_deptno NUMBER:=50;
BEGIN
IF(:NEW.deptno=v_deptno ) THEN
INSERT INTO dept(DEPTNO, DNAME, LOC)VALUES (50,'DEFENCE','INDIANA');
COMMIT;
RAISE_APPLICATION_ERROR(-20000,'Match found can not be inserted');
END IF;
end;
--try with what is matching record
INSERT INTO emp(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES(7800,'SAHEED','MANAGER',7839,SYSDATE,3000,NULL,50);
--try with what is not matching
INSERT INTO emp(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
VALUES(7800,'SAHEED','MANAGER',7839,SYSDATE,3000,NULL,60);
SELECT * FROM dept;
SELECT * FROM emp;
|
|
|
|
|
|
|
|
|
|
|
|
| Re: How to restrict the inserting a record through a oracle trigger [message #634345 is a reply to message #634342] |
Sun, 08 March 2015 10:12   |
 |
Kevin Meade
Messages: 2103 Registered: December 1999 Location: Connecticut USA
|
Senior Member |
|
|
I don't think a table based trigger will work for this requirement. Seems to me that if you need to "remove" the row in table A after inserting it, then this would cause a MUTATING TABLE error. Let me check. Yep.
USER SYSDATE NAME INSTANCE_NUMBER INSTANCE_NAME HOST_NAME VERSION
------------------------------ -------------------- --------- --------------- ---------------- ------------------------------ -------------
KEVIN 08-mar-2015 11:11:12 ORCL 1 orcl FORESE 11.2.0.1.0
1 row selected.
Elapsed: 00:00:00.01
11:11:12 SQL> drop table a;
Table dropped.
Elapsed: 00:00:00.07
11:11:17 SQL>
11:11:17 SQL> create table a (aid integer not null);
Table created.
Elapsed: 00:00:00.01
11:11:17 SQL>
11:11:17 SQL> create or replace trigger air_a
11:11:17 2 after insert on a
11:11:17 3 for each row
11:11:17 4 begin
11:11:17 5 delete from a where aid = :new.aid;
11:11:17 6 end;
11:11:17 7 /
Trigger created.
Elapsed: 00:00:00.01
11:11:17 SQL> show errors;
No errors.
11:11:17 SQL>
11:11:17 SQL> insert into a values (1);
insert into a values (1)
*
ERROR at line 1:
ORA-04091: table KEVIN.A is mutating, trigger/function may not see it
ORA-06512: at "KEVIN.AIR_A", line 2
ORA-04088: error during execution of trigger 'KEVIN.AIR_A'
Elapsed: 00:00:00.01
Yep, this has not changed since triggers were first introduced. So if the OP has a trigger based solution, it ain't deleting from the table the trigger is defined on.
Kevin
[Updated on: Sun, 08 March 2015 10:29] Report message to a moderator
|
|
|
|
|
|
| Re: How to restrict the inserting a record through a oracle trigger [message #634347 is a reply to message #634346] |
Sun, 08 March 2015 10:59   |
John Watson
Messages: 9003 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
I hate triggers so much. You do something, and without your knowledge something totally unrelated occurs. You can't get much further from the relational paradigm than that. Except including an autonomous transaction in the trigger!
If one were working in C, this would be called a "side effect". And programmers who write code with side effects shold be shot.
|
|
|
|
|
|
|
|
| Re: How to restrict the inserting a record through a oracle trigger [message #634381 is a reply to message #634303] |
Mon, 09 March 2015 03:30   |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
sss111ind wrote on Sat, 07 March 2015 14:37
IF(:NEW.deptno=v_deptno ) THEN
INSERT INTO dept(DEPTNO, DNAME, LOC)VALUES (50,'DEFENCE','INDIANA');
COMMIT;
RAISE_APPLICATION_ERROR(-20000,'Match found can not be inserted');
END IF;
I don't understand the logic here. You first insert and commit as autonomous transaction, and then also raise an error saying it cannot be inserted. Makes no sense.
|
|
|
|
|
|