GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Oracle  >  PL/SQL
Go To First  |  Previous Question  |  Next Question 
 PL/SQL  |  Question 165 of 241    Print  
how to avoid the mutating error with sample program
Hi,
can any body give me the sample program for avoiding the mutating error. we can avoid the mutating through the statement level trigger instead of using the statement leve trigger, i need one sample program to avoid the mutating error by using pl/sql table.

Thanks
Saravanan.P



  
Total Answers and Comments: 1 Last Update: May 02, 2007     Asked by: ily_saravanan 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: KiranKW
 

A mutating error comes when u r trying to do some DML operation on a table that owns the trigger.... there are a lot of options to avoid mutation .. eg, Making it autonomous transaction or making it a statement level trigger ... or using temporary table... the following example gives how to use temporary pl/sql table to avoid mutation error :

        
create table CUG (
  id_cug      number(12) not null primary key,
  id_B        number(12) not null,
  type        number(1),
foreign key (id_B) references CUG (id_cug)
on delete cascade
);

Next we create a temporary table to avoid the "Mutating Table Problem".

drop table CUGTMP;
create global temporary table CUGTMP (
  id_B        number(12),
  type        number(1))
on commit delete rows;

The following trigger checks new rows (Inserts) in CUG

create or replace trigger bi_r
before insert on CUG
for each row
declare
  l_type     CUG.type%type;
begin
  if (:new.type in (3,4)) then
    select type into l_type from CUG
     where id_cug = :new.id_B;
  end if;
  if (l_type != 2) then
     raise_application_error(-20002,
     'C and D CUGs must have a leading B');
  end if;
end;
/

The following Trigger saves the new values for id_B in the temporary table.

create or replace trigger au_r
after update of id_B on CUG
for each row
begin
  insert into CUGTMP (id_B,type)
  values (:new.id_B,:new.type);
end;
/

The following Trigger finally checks, that C and D CUGs belong to a B CUG.

create or replace trigger au_s
after update of id_B on CUG
declare
  l_id_B        number(12);
  l_typeCD      number(1);
  l_typeB       number(1);
  cursor cur_cugtmp is
  select id_B,type
   from CUGTMP;
begin
  open cur_cugtmp;
  loop
    fetch cur_cugtmp into l_id_B,l_typeCD;
    exit when cur_cugtmp%notfound;
     select type into l_typeB from CUG
      where id_cug = l_id_B;
    if (l_typeB != 2) then
       raise_application_error(-20002,
       'C and D CUGs must have a leading B');
    end if;
  end loop;
  close cur_cugtmp;
end;
/

Test insert and update

insert into CUG (id_cug,id_B,type)
  values (0,0,0);
insert into CUG (id_cug,id_B,type)
  values (1,0,2);
insert into CUG (id_cug,id_B,type)
  values (2,0,2);
insert into CUG (id_cug,id_B,type)
  values (3,1,3);
insert into CUG (id_cug,id_B,type)
  values (4,2,3);
insert into CUG (id_cug,id_B,type)
  values (5,1,4);
insert into CUG (id_cug,id_B,type)
  values (6,2,4);
commit;

SQL> select * from CUG;

    ID_CUG       ID_B       TYPE
---------- ---------- ----------
         0          0          0
         1          0          2
         2          0          2
         3          1          3
         4          2          3
         5          1          4
         6          2          4

Now, we want that that the CUGs 3,4,5,6 changes the leadership to CUG 2

SQL> update CUG set id_B = 2 where id_cug in (3,4,5,6);

4 rows updated.

SQL> select * from cug;

    ID_CUG       ID_B       TYPE
---------- ---------- ----------
         0          0          0
         1          0          2
         2          0          2
         3          2          3
         4          2          3
         5          2          4
         6          2          4

Next we delete the "Leader" with ID_CUG = 2. All childs must be deleted automatically with the DELETE CASCADE.

SQL> select * from cug;

    ID_CUG       ID_B       TYPE
---------- ---------- ----------
         0          0          0
         1          0          2

Everything looks fine - cool isn't it ?



Above answer was rated as good by the following members:
radheshyambidada
May 02, 2007 21:54:32   #1  
KiranKW Member Since: May 2007   Contribution: 7    

RE: how to avoid the mutating error with sample progra...

A mutating error comes when u r trying to do some DML operation on a table that owns the trigger.... there are a lot of options to avoid mutation .. eg Making it autonomous transaction or making it a statement level trigger ... or using temporary table... the following example gives how to use temporary pl/sql table to avoid mutation error :


create table CUG (
id_cug number(12) not null primary key
id_B number(12) not null
type number(1)
foreign key (id_B) references CUG (id_cug)
on delete cascade
);

Next we create a temporary table to avoid the "Mutating Table Problem".

drop table CUGTMP;
create global temporary table CUGTMP (
id_B number(12)
type number(1))
on commit delete rows;

The following trigger checks new rows (Inserts) in CUG

create or replace trigger bi_r
before insert on CUG
for each row
declare
l_type CUG.type type;
begin
if (:new.type in (3 4)) then
select type into l_type from CUG
where id_cug :new.id_B;
end if;
if (l_type ! 2) then
raise_application_error(-20002
'C and D CUGs must have a leading B');
end if;
end;
/

The following Trigger saves the new values for id_B in the temporary table.

create or replace trigger au_r
after update of id_B on CUG
for each row
begin
insert into CUGTMP (id_B type)
values (:new.id_B :new.type);
end;
/

The following Trigger finally checks that C and D CUGs belong to a B CUG.

create or replace trigger au_s
after update of id_B on CUG
declare
l_id_B number(12);
l_typeCD number(1);
l_typeB number(1);
cursor cur_cugtmp is
select id_B type
from CUGTMP;
begin
open cur_cugtmp;
loop
fetch cur_cugtmp into l_id_B l_typeCD;
exit when cur_cugtmp notfound;
select type into l_typeB from CUG
where id_cug l_id_B;
if (l_typeB ! 2) then
raise_application_error(-20002
'C and D CUGs must have a leading B');
end if;
end loop;
close cur_cugtmp;
end;
/

Test insert and update

insert into CUG (id_cug id_B type)
values (0 0 0);
insert into CUG (id_cug id_B type)
values (1 0 2);
insert into CUG (id_cug id_B type)
values (2 0 2);
insert into CUG (id_cug id_B type)
values (3 1 3);
insert into CUG (id_cug id_B type)
values (4 2 3);
insert into CUG (id_cug id_B type)
values (5 1 4);
insert into CUG (id_cug id_B type)
values (6 2 4);
commit;

SQL> select * from CUG;

ID_CUG ID_B TYPE
---------- ---------- ----------
0 0 0
1 0 2
2 0 2
3 1 3
4 2 3
5 1 4
6 2 4

Now we want that that the CUGs 3 4 5 6 changes the leadership to CUG 2

SQL> update CUG set id_B 2 where id_cug in (3 4 5 6);

4 rows updated.

SQL> select * from cug;

ID_CUG ID_B TYPE
---------- ---------- ----------
0 0 0
1 0 2
2 0 2
3 2 3
4 2 3
5 2 4
6 2 4

Next we delete the "Leader" with ID_CUG 2. All childs must be deleted automatically with the DELETE CASCADE.

SQL> select * from cug;

ID_CUG ID_B TYPE
---------- ---------- ----------
0 0 0
1 0 2

Everything looks fine - cool isn't it ?


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    

 Related Questions

 Database  trigger  is stored PL/SQL program unit associated with a specific database   table.   Usages   are  Audit  data  modifications,  
Latest Answer : A database trigger is a named pl/sql block associated with a table and fires automatically when an event occurs or something happens. Data auditing , Implementing complex business rules, security are main uses of database triggers. ...

 Exception  is  the  error  handling  part  of  PL/SQL  block. The types are Predefined and user defined. Some of Predefined exceptions are.      
Latest Answer : Hi all, Exception is nothing but Error. Exception can serve as an ALERT message also. (using RAISE_APPLICATION_ERROR)There are two types of exceptions: 1> Pre-defineddefine exception (2> User define exception. System define exception predefine ...

 The PRAGMA EXECPTION_INIT tells the complier to associate an exception with an oracle error. To get an error message of a specific oracle error.      e.g. PRAGMA EXCEPTION_INIT 
Latest Answer : PRAGMA EXCEPTION_INIT statement associate the declared exception with the standard Oracle server error number. PRAGMA EXCEPTION_INIT tells the compiler to associate an exception name with an Oracle error number. That allows you to refer to any internal ...

 Raise_application_error  is  a  procedure  of  package  DBMS_STANDARD which allows  to  issue an user_defined error messages from stored sub-program or database 
Latest Answer : You can use this procedure to issue user-defined error messages from stored subprograms.You can report errors to your application and avoid returning unhandled exceptions.Syntax: raise_application_error (error_number,message[, {TRUE | FALSE}]); ...

 SQLCODE returns the latest code of the error that has occurred.SQLERRM returns the relevant error message of the SQLCODE. 
Latest Answer : • SQLCODE: Returns the numeric value for the error code• SQLERRM: Returns the message associated with the error numberSQLCODE Value                  Description ...

     a. Stored procedure or anonymous block     b. an application program such a PRC *C, PRO* COBOL     c. SQL *PLUS      
Latest Answer : a. PACKAGE NAME.PROCEDURE NAME (parameters);     variable := PACKAGE NAME.FUNCTION NAME (arguments);        b.     BEGIN     PACKAGE NAME.PROCEDURE ...

What is the output of the following pl/sql block ?declare v_empno emp.empno%type;begin select empno into v_empno from emp where empno = 10;exception when others then dbms_output.put_line ( 'no data found'); when no_data_found then dbms_output.put_line ( 'ther is no data found ');end;
when others then *ERROR at line 6:ORA-06550: line 6, column 2:PLS-00370: OTHERS handler must be last among the exception handlers of a blockORA-06550: line 0, column 0:PL/SQL: Compilation unit analysis 
Read Answers (5) | Asked by : Kishorebabu

You have compiled some PL/SQL packages in your schema, and found aome errors in one procedure.how do you find which procedure produced the error?how do you find which section of the code produced the error and look at?
no answer 
Read Answers (9) | Asked by : Mohammad

View Question | Asked by : Nitina

1)what is the starting "oracle error number"?2)what is meant by forward declaration in functions?
Read Answers (7) | Asked by : suribabu


 Sponsored Links

 
Related Articles

Querying Data with Oracle XQuery

Querying Data with Oracle XQuery Starting with Oracle Database 10g Release 2 you can take advantage of a full featured native XQuery engine integrated with the database With Oracle XQuery you can accomplish various tasks involved in developing PHP Oracle XML applications operating on any kind of dat
 

Using Oracle XML DB Repository

Using Oracle XML DB Repository Another variation on accessing and manipulating XML content stored in Oracle database is provided by Oracle XML DB repository which is an essential component of Oracle XML DB mosgoogle NOTE Oracle XML DB repository also known as XML repository is a hierarchically organ
 

Using Oracle Database for Storing, Modifying, and Retrieving XML Data

Using Oracle Database for Storing Modifying and Retrieving XML Data With Oracle XML DB you have various XML storage and XML processing options allowing you to achieve the required level of performance and scalability One of the most interesting things about Oracle XML DB is that it allows you to per
 

XML Processing in PHP and Oracle Applications

Processing XML in PHP Oracle Applications As mentioned there are two alternatives when it comes to performing XML processing in your PHP Oracle application You can perform any required XML processing using either PHP s XML extensions or PEAR XML packages or Oracle s XML features mosgoogle In the fol
 

PHP Oracle Web Development

PHP Oracle Web Development Data processing Security Caching XML Web Services and Ajax The book is written by Yuli Vaseliev a well known author of different web development and programming books PHP Oracle Web Development Data processing Security Caching XML Web Services and Ajax is a good starting b
 

Getting Started with Oracle and ODP.NET

ODP NET Developer’ s Guide by Jagadish Chatarji Pulakhandam Sunitha Paruchuri A practical guide for developers working with the Oracle Data Provider for NET and the Oracle Developer Tools for Visual Studio 2005 Application development with ODP NET Dealing with XML DB using ODP NET Oracle
 

PHP Oracle Web Development Review

PHP Oracle Web Development Data processing Security Caching XML Web Services and Ajax The book is written by Yuli Vaseliev a well known author of different web development and programming books The author is also an expert in open source technologies and SOA Service Oriented Architecture But besides
 

Step by Step Oracle PL-SQL Tutorial

This introductory tutorial to PL SQL will help you to understand the basic concepts of PL SQL Please review the following tutorials and practice the sample SQL Statements on your local Oracle Database Please note that you must learn these basic things before we actually start getting in to Advanced
 

Working with XML in Oracle

Working with XML in Oracle Introduction to XML Extensive markup language is the language which presents data in a human readable form of text The data can be anything from a purchase order or a stock quote or weather radar or a flight schedule it can be represented using XML XML is very similar to H
 

What is Oracle Net Service

Oracle Net Services provides enterprise-wide connectivity solutions in distributed, heterogeneous computing environments. Oracle Net Services eases the complexities of network configuration and management, maximizes performance, and improves network diagnostic capabilities. Oracle Net, a component
 

About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape