Home » SQL & PL/SQL » SQL & PL/SQL » How to restrict the inserting a record through a oracle trigger (oracle 10g)
How to restrict the inserting a record through a oracle trigger [message #634293] Sat, 07 March 2015 01:59 Go to next message
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 #634295 is a reply to message #634293] Sat, 07 March 2015 02:06 Go to previous messageGo to next message
John Watson
Messages: 9003
Registered: January 2010
Location: Global Village
Senior Member
Welcome to the forum. Please read our OraFAQ Forum Guide and How to use [code] tags and make your code easier to read

You are probably taking completely the wrong apprach. You do not need PL/SQL for this. Better would be a multi-table INSERT,
http://docs.oracle.com/database/121/SQLRF/statements_9014.htm#sthref7087
Re: How to restrict the inserting a record through a oracle trigger [message #634297 is a reply to message #634295] Sat, 07 March 2015 02:17 Go to previous messageGo to next message
shivas1177
Messages: 5
Registered: March 2015
Junior Member
Hi John,
Thanks for quick reply.

I am doing inserting records into table a by procedeure. This procedure calls in informatica.

In table b records are say 100,101
in table c records are say 101,102
If they are matching with new values then will insert into table d and restrict these records into table a.

Can you show me the code please

Thanks,
Shiva

Re: How to restrict the inserting a record through a oracle trigger [message #634298 is a reply to message #634297] Sat, 07 March 2015 02:20 Go to previous messageGo to next message
John Watson
Messages: 9003
Registered: January 2010
Location: Global Village
Senior Member
So write your procedure to call a multitable insert statement.

If you want more help, you must follow the forum guidelines (which you clearly have not yet read) and post the test case: the CREATE TBLE and INSERT statements needed to set up your problem.

By the way, I won't write your code for you (you would have to pay for consulting for that).

[Updated on: Sat, 07 March 2015 02:21]

Report message to a moderator

Re: How to restrict the inserting a record through a oracle trigger [message #634300 is a reply to message #634298] Sat, 07 March 2015 02:47 Go to previous messageGo to next message
shivas1177
Messages: 5
Registered: March 2015
Junior Member
SQL> create table a(id number,id1 number);

Table created.

Elapsed: 00:00:00.31
SQL> create table b(id number);

Table created.

Elapsed: 00:00:00.06
SQL> create table c(id number);

Table created.
SQL> insert into b values(100);

1 row created.

Elapsed: 00:00:00.01
SQL> insert into c values(101);

1 row created.

Elapsed: 00:00:00.00
SQL> create table d(id number, id1 number);

Table created.

Elapsed: 00:00:00.04
SQL> create or replace trigger trg_a
2 before insert on a
3 for each row
4 declare
5 v_id b.id%type;
6 v_id1 c.id1%type;
7 pragma autonomous_transaction;
8 begin
9 select id into v_id from b where id=:NEW.id;
10 select id1 into v_id1 from c where id1=:NEW.id1;
11 if(:NEW.id=v_id or :NEW.id1=v_id1) then
12 insert into d
13 (id,id1)
14 values(:NEW.id,:NEW.id1);
15 end if;
16 delete from a where id =v_id or id1=v_id1;
17 commit;
18 exception
19 when others then
20 raise;
21 end;
22 /

Trigger created.

SQL> select * from d;

no rows selected

Elapsed: 00:00:00.00
SQL> select * from b;

ID
----------
100

Elapsed: 00:00:00.00
SQL> select * from c;

ID1
----------
101

Elapsed: 00:00:00.00
SQL> select * from a;

no rows selected

Elapsed: 00:00:00.00
SQL> insert into a values(100,101);

1 row created.

Elapsed: 00:00:00.02
SQL> select * from d;

ID ID1
---------- ----------
100 101

Elapsed: 00:00:00.00
SQL> select * from a;

ID ID1
---------- ----------
100 101

Elapsed: 00:00:00.00
SQL>
Above matchin record only insert into table d. But here inserting trg_a into a also.

Please help

Re: How to restrict the inserting a record through a oracle trigger [message #634302 is a reply to message #634300] Sat, 07 March 2015 03:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

Post the test case and its execution we don't care.
We can't copy and paste the execution listing of a script to build your test case in our side:
SQL> SQL> create table a(id number,id1 number);
SP2-0734: unknown command beginning "SQL> creat..." - rest of line ignored.
SQL>
SQL> Table created.
SP2-0734: unknown command beginning "Table crea..." - rest of line ignored.
SQL>
SQL> Elapsed: 00:00:00.31
SP2-0734: unknown command beginning "Elapsed: 0..." - rest of line ignored.
SQL> SQL> create table b(id number);
SP2-0734: unknown command beginning "SQL> creat..." - rest of line ignored.
SP2-0044: For a list of known commands enter HELP
and to leave enter EXIT.

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 Go to previous messageGo to next message
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 #634306 is a reply to message #634303] Sat, 07 March 2015 03:11 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

What in the hell is this stupid trigger?

Re: How to restrict the inserting a record through a oracle trigger [message #634340 is a reply to message #634306] Sun, 08 March 2015 04:47 Go to previous messageGo to next message
shivas1177
Messages: 5
Registered: March 2015
Junior Member
Hi All,

I have solved my above problem with trigger..

Thanks
Shiva
Re: How to restrict the inserting a record through a oracle trigger [message #634341 is a reply to message #634340] Sun, 08 March 2015 05:48 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

OraFAQ Forum Guide

12. If you found an answer yourself, post it. That way we know the issue is resolved and we might learn from it.

Re: How to restrict the inserting a record through a oracle trigger [message #634342 is a reply to message #634340] Sun, 08 March 2015 06:25 Go to previous messageGo to next message
EdStevens
Messages: 1377
Registered: September 2013
Senior Member
shivas1177 wrote on Sun, 08 March 2015 04:47
Hi All,

I have solved my above problem with trigger..

Thanks
Shiva


Perhaps. But it is not at all clear that a trigger is even required.
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 Go to previous messageGo to next message
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 #634346 is a reply to message #634345] Sun, 08 March 2015 10:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Kevin,

You missed this part in all posted code:

Quote:
pragma autonomous_transaction;


which workarounds the error but turns the trigger to be wrong, of course.

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 Go to previous messageGo to next message
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 #634348 is a reply to message #634347] Sun, 08 March 2015 11:57 Go to previous messageGo to next message
shivas1177
Messages: 5
Registered: March 2015
Junior Member
Hi all,

I have created a view on the table a used insted of trigger to restrict the row being inserted..

Thank you.

Regards,
Shiva
Re: How to restrict the inserting a record through a oracle trigger [message #634349 is a reply to message #634348] Sun, 08 March 2015 11:58 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://www.orafaq.com/forum/mv/msg/196665/634341/#msg_634341
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 Go to previous messageGo to next message
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.
Re: How to restrict the inserting a record through a oracle trigger [message #634383 is a reply to message #634381] Mon, 09 March 2015 04:10 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

The whole topic makes no sense.
And OP refused to post his "solution"; I don't think we will see him again.

Previous Topic: SQLPLUS PLS-00103 Encountered 'INSERT'
Next Topic: How to remove the NULL values from SELECT clause
Goto Forum:
  


Current Time: Thu Aug 27 01:06:26 CDT 2026