Skip to main content

INSTEAD OF TRIGGER EXAMPLE

 INSTEAD OF TRIGGER EXAMP


Instead of trigger is used to provide a  way of modifying views that cannot be modified directly through SQL DML statements because the view is not inherently modifiable. 

ex :- composite views . , views that are based on two tables with aggregation functions and etc.

You can write INSERT, UPDATE, and DELETE statements against the view.

 The INSTEAD OF trigger works invisibly in the background performing the action coded in the trigger body directly on the underlying tables.


example :- the following is a complex view and cannot be modified .

create view complex_view as 

select  e.deptno,d.dname,sum(e.Sal) sum_sal

from emp e join dept d on (e.deptno=d.deptno) 

group by e.deptno,d.dname


SQL>  select * from complex_view;


    DEPTNO DNAME             SUM_SAL

---------- -------------- ----------

        10 software             9804

        20 RESEARCH            10985

        30 SALES                9401

Try to modify the conent of the view by using an update statement, it fails.


sQL> update complex_view set dname='sale' where dname ='SALES';

update complex_view set dname='IT' where dname ='SALES'

       *

ERROR at line 1:

ORA-01732: data manipulation operation not legal on this view


An instead of trigger can be used to modify the view, however the instead of trigger too cannot directly moidfy the view, so a workaround is done in the code and the base tables are modified to later reflect in the complex view.


create or replace trigger instead_of_trigger 

instead of update on complex_view for each row

begin

update dept set dname = :new.dname where dname= :old.dname;

end;


SELECT * FROM DEPT; -- FOR VERIFICATION


update complex_view set dname='IT' where dname ='SALES';



SELECT * FROM  COMPLEX_VIEW;-- FOR VERIFICATION


Comments

Ankit Gupta said…
simplest way of instead of trigger
very helpful
jathin said…
Good explanation on how to use "instead of trigger".
Venu Gopal said…
Good description and detailed example on instead of trigger
nikhil said…
Great explanation—clear and concise!
Lakshman said…
clear understanding about instead of triggers

Popular posts from this blog

Oracle Merge Statement Delta Detection

The MERGE Syntax by Ajay Nerumati Delta Detection in Oracle SQL Posted on  8. October 2016 Delta detection is a common task in every Data Warehouse. It compares new data from a source system with the last versions in the Data Warehouse to find out whether a new version has to be created. There are several ways to implement this in Oracle. Your source system delivers a full extraction every night, and you have to load only the changed rows into your Core Data Warehouse? You receive incremental loads from another source system every few minutes, but only a few columns are loaded into the Data Warehouse. In all these situations, you need a delta detection mechanism to identify the rows that have to be inserted or updated in your Data Warehouse tables. In this blog post, I want to show different methods in Oracle SQL that provide the subset of rows of a source table that were changed since the last load. All these methods are set-based, i.e. they can be executed in one SQL sta...

RDBMS MINI PROJECT

  Capgemini class room training.   RDBMS MINI PROJECT ( SPRINT ) LIBRARY MANAGEMENT SYSTEM   Table of Contents Serial No. Topic Name Content Page No. 1.   Introduction 1.1 Setup checklist for mini project 3     1.2 Instructions 3 2.   Problem statement   2.1 Objective 4     2.2 Abstract of the project 4     2.3 Functional components of the project 4     2.4 Technology used 5 3.   Implementation in RDBMS LOT 3.1 Guidelines on the functionality to be built 6 4.   Evaluation 4.1 Evaluation 7   ...

PL / SQL practice programs

  PL / SQL  practice programs 1. Write a program to print the following format WELCOME TO PL/SQL PROGRAMMING   BEGIN   DBMS_OUTPUT.PUT_LINE('WELCOME   TO   PL/SQL   PROGRAMMING'); END; /   2. Write a program to print the numbers from 1 to 100   DECLARE N NUMBER(3):=1; V VARCHAR2(1000); BEGIN WHILE N <=1000 LOOP V:=V||''||N; N:=N+1; END LOOP; DBMS_OUTPUT.PUT_LINE(V); END; / 3. write a program to print the even numbers from 1 to 100 DECLARE N NUMBER(3):=0; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 4. Write a program to print the odd numbers from 1 to 100 DECLARE N NUMBER(3):=1; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 5. write a program for multiplication table DECLARE A NUMBER(2):=&A; B   NUMBER(2):=1; C ...