Create a trigger instead of update several tables in a view

Dear everybody

I am trying to create a trigger that updates instead of to day joined several tables in a view, but I can't get my trigger to work. The create view command was as follows:
CREATE OR REPLACE VIEW VIEW_MI_JOIN_GC
AS
SELECT MAP_INDEX.mi_prinx,
       index_type_id,
       original_map_publication_id,
       original_map_sheet_number_id,
       name_of_feature,
       geog_coordinates_id,
       GEOG_COORDINATES.mi_prinx AS "mi_prinx_polygon",
       GEOG_COORDINATES.geographical_coordinates,
       GEOG_COORDINATES.mapinfo_style_row
 FROM MAP_INDEX
  JOIN GEOG_COORDINATES
   ON geog_coordinates_id=GEOG_COORDINATES.mi_prinx;
The view above connects a polygon table to the table of function names which means that a polygon appears several times in a view, even though one version of the polygon exists in the base table. This means the direct update of view cannot take place, since 1 polygon can appear multiple times in a view. The two original base tables and their columns names are:
MAP_INDEX
---------
MI_PRINX
INDEX_TYPE_ID
ORIGINAL_MAP_PUBLICATION_ID
ORIGINAL_MAP_SHEET_NUMBER_ID
NAME_OF_FEATURE
MAPINFO_STYLE_ROW
GEOGRAPHICAL_COORDINATES
GEOG_COORDINATES_ID

GEOG_COORDINATES
----------------
MI_PRINX
GEOGRAPHICAL_COORDINATES
MAPINFO_STYLE_ROW
Relax, I created was as follows:
CREATE OR REPLACE TRIGGER TRIG_VIEW_MI_JOIN_GC
   INSTEAD OF UPDATE ON VIEW_MI_JOIN_GC
      REFERENCING NEW AS NEW
 FOR EACH ROW
BEGIN
 UPDATE MAP_INDEX
  SET mi_prinx = :NEW.mi_prinx,
      index_type_id = :NEW.index_type_id,
      original_map_publication_id = :NEW.original_map_publication_id,
      original_map_sheet_number_id = :NEW.original_map_sheet_number_id,
      name_of_feature = :NEW.name_of_feature,
      mapinfo_style_row = :NEW.mapinfo_style_row,
      geographical_coordinates = :NEW.geographical_coordinates,
      geog_coordinates_id = :NEW.geog_coordinates_id
   WHERE geog_coordinates_id = :OLD.geog_coordinates_id;
 UPDATE GEOG_COORDINATES
  SET mi_prinx = :NEW.mi_prinx,
      geographical_coordinates = :NEW.geographical_coordinates,
      mapinfo_style_row = :NEW.mapinfo_style_row
   WHERE mi_prinx = :OLD.mi_prinx;
END;
/
The idea is that when I draw a new polygon in MapInfo and assign him a revised number geog_coordinates_id and the number of mi_prinx_polygon, which are the same, once I have save the view as then it updates the underlying tables. Geographical_coordinates and mapinfo_style_row of map_index table columns are columns with ancient polygon data which while not having currently new data inserted into them, are required for the previous data they contain. These data are currently being added to the geog_coordinates table with other scripts. The idea is that all the data is then read using a view and updates made to the view, triggering instead of relaxing, so data are not duplicated but still visible as if it were.

When I created first the relaxation above in SQLdeveloper it seems to run constantly. Then my computer crashed, not related to this work, and I lost session because I did not commit it. I was not ready to commit it because I believe that something is wrong.

I am in the trigger syntax correctly and I go about it in the right way? I want to only update the rows that have changed, that's why I was using: NEWS and: OLD.

Kind regards

Tim

Published by: user467357 on November 18, 2008 18:07
I modified my script a little because there were a few errors. for example. start and old as old and view name typo

Something like this->

satyaki>
satyaki>select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
PL/SQL Release 10.2.0.3.0 - Production
CORE    10.2.0.3.0      Production
TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production

Elapsed: 00:00:01.78
satyaki>
satyaki>
satyaki>create table MAP_INDEX
  2   (
  3        mi_prinx                    NUMBER(10) not null,
  4        index_type_id                NUMBER(6) not null,
  5        original_map_publication_id  NUMBER(6) not null,
  6        original_map_sheet_number_id NUMBER(6) not null,
  7        name_of_feature              VARCHAR2(80) not null,
  8        mapinfo_style_row            VARCHAR2(80),
  9        geographical_coordinates    SDO_GEOMETRY,
 10        geog_coordinates_id          NUMBER(10),
 11        constraints pk_mi_prinx primary key(mi_prinx)
 12   );

Table created.

Elapsed: 00:00:04.39
satyaki>
satyaki>create table GEOG_COORINDATES
  2   (
  3     mi_prinx NUMBER(10) not null,
  4     geographical_coordinates SDO_GEOMETRY,
  5     mapinfo_style_row VARCHAR2(80),
  6     constraints pk_mi_prinx_n primary key(mi_prinx)
  7   );

Table created.

Elapsed: 00:00:00.30
satyaki>
satyaki>
satyaki>CREATE OR REPLACE VIEW VIEW_MI_JOIN_GC
  2  AS
  3  SELECT MAP_INDEX.mi_prinx,
  4         MAP_INDEX.index_type_id,
  5         MAP_INDEX.original_map_publication_id,
  6         MAP_INDEX.original_map_sheet_number_id,
  7         MAP_INDEX.name_of_feature,
  8         MAP_INDEX.geog_coordinates_id,
  9         GEOG_COORINDATES.mi_prinx AS "mi_prinx_polygon",
 10         GEOG_COORINDATES.geographical_coordinates,
 11         GEOG_COORINDATES.mapinfo_style_row
 12   FROM MAP_INDEX , GEOG_COORINDATES
 13   WHERE MAP_INDEX.geog_coordinates_id=GEOG_COORINDATES.mi_prinx;

View created.

Elapsed: 00:00:00.32
satyaki>
satyaki>
satyaki>insert into MAP_INDEX values(
  2                                 &mi_prinx,
  3                                 &index_type_id,
  4                                 &original_map_publication_id,
  5                                 &original_map_sheet_number_id,
  6                                 '&name_of_feature',
  7                                 '&mapinfo_style_row',
  8                                 null,
  9                                 &geog_coordinates_id);
Enter value for mi_prinx: 1
old   2:                                &mi_prinx,
new   2:                                1,
Enter value for index_type_id: 44
old   3:                                &index_type_id,
new   3:                                44,
Enter value for original_map_publication_id: 5678
old   4:                                &original_map_publication_id,
new   4:                                5678,
Enter value for original_map_sheet_number_id: 356
old   5:                                &original_map_sheet_number_id,
new   5:                                356,
Enter value for name_of_feature: AA
old   6:                                '&name_of_feature',
new   6:                                'AA',
Enter value for mapinfo_style_row: GG
old   7:                                '&mapinfo_style_row',
new   7:                                'GG',
Enter value for geog_coordinates_id: 9
old   9:                                &geog_coordinates_id)
new   9:                                9)

1 row created.

Elapsed: 00:00:00.16
satyaki>/
Enter value for mi_prinx: 2
old   2:                                &mi_prinx,
new   2:                                2,
Enter value for index_type_id: 55
old   3:                                &index_type_id,
new   3:                                55,
Enter value for original_map_publication_id: 6789
old   4:                                &original_map_publication_id,
new   4:                                6789,
Enter value for original_map_sheet_number_id: 357
old   5:                                &original_map_sheet_number_id,
new   5:                                357,
Enter value for name_of_feature: BB
old   6:                                '&name_of_feature',
new   6:                                'BB',
Enter value for mapinfo_style_row: 10
old   7:                                '&mapinfo_style_row',
new   7:                                '10',
Enter value for geog_coordinates_id: 8
old   9:                                &geog_coordinates_id)
new   9:                                8)

1 row created.

Elapsed: 00:00:00.04
satyaki>
satyaki>commit;

Commit complete.

Elapsed: 00:00:00.03
satyaki>
satyaki>
satyaki>insert into GEOG_COORINDATES values(&mi_prinx,null,'&mapinfo_style_row');
Enter value for mi_prinx: 9
Enter value for mapinfo_style_row: FFG
old   1: insert into GEOG_COORINDATES values(&mi_prinx,null,'&mapinfo_style_row')
new   1: insert into GEOG_COORINDATES values(9,null,'FFG')

1 row created.

Elapsed: 00:00:00.07
satyaki>/
Enter value for mi_prinx: 8
Enter value for mapinfo_style_row: GGT
old   1: insert into GEOG_COORINDATES values(&mi_prinx,null,'&mapinfo_style_row')
new   1: insert into GEOG_COORINDATES values(8,null,'GGT')

1 row created.

Elapsed: 00:00:00.05
satyaki>
satyaki>commit;

Commit complete.

Elapsed: 00:00:00.02
satyaki>
satyaki>select * from VIEW_MI_JOIN_GC;

  MI_PRINX INDEX_TYPE_ID ORIGINAL_MAP_PUBLICATION_ID ORIGINAL_MAP_SHEET_NUMBER_ID NAME_OF_FEATURE                                                                  GEOG_COORDINATES_ID mi_prinx_polygon GEOGRAPHICAL_COORDINATES(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES)
---------- ------------- --------------------------- ---------------------------- -------------------------------------------------------------------------------- ------------------- ---------------- ---------------------------------------------------------------------------------------------------------------
         1            44                        5678                          356 AA                                                                                                 9                9
         2            55                        6789                          357 BB                                                                                                 8                8                                                                                                                 

Elapsed: 00:00:00.09
satyaki>
satyaki>
satyaki>CREATE OR REPLACE TRIGGER TRIG_VIEW_MI_JOIN_GC
  2  INSTEAD OF UPDATE ON VIEW_MI_JOIN_GC
  3  FOR EACH ROW
  4  DECLARE
  5   m_info_svw  varchar2(80);
  6  BEGIN
  7   m_info_svw :=  :NEW.mapinfo_style_row;
  8
  9   UPDATE MAP_INDEX
 10    SET mi_prinx = :NEW.mi_prinx,
 11        index_type_id = :NEW.index_type_id,
 12        original_map_publication_id = :NEW.original_map_publication_id,
 13        original_map_sheet_number_id = :NEW.original_map_sheet_number_id,
 14        name_of_feature = :NEW.name_of_feature,
 15        mapinfo_style_row = m_info_svw,
 16        geographical_coordinates = :NEW.geographical_coordinates,
 17        geog_coordinates_id = :NEW.geog_coordinates_id
 18     WHERE geog_coordinates_id = :OLD.geog_coordinates_id;
 19   UPDATE GEOG_COORINDATES
 20    SET mi_prinx = :NEW.geog_coordinates_id,
 21        geographical_coordinates = :NEW.geographical_coordinates,
 22        mapinfo_style_row = m_info_svw
 23     WHERE mi_prinx = :OLD.geog_coordinates_id;
 24  END;
 25  /

Trigger created.

Elapsed: 00:00:00.20
satyaki>
satyaki>select * from VIEW_MI_JOIN_GC;

  MI_PRINX INDEX_TYPE_ID ORIGINAL_MAP_PUBLICATION_ID ORIGINAL_MAP_SHEET_NUMBER_ID NAME_OF_FEATURE                                                                  GEOG_COORDINATES_ID mi_prinx_polygon GEOGRAPHICAL_COORDINATES(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES)
---------- ------------- --------------------------- ---------------------------- -------------------------------------------------------------------------------- ------------------- ---------------- ---------------------------------------------------------------------------------------------------------------
         1            44                        5678                          356 CC                                                                                                 9                9
         2            55                        6789                          357 BB                                                                                                 8                8                                                                                                                 

Elapsed: 00:00:00.09
satyaki>
satyaki>select mi_prinx,mapinfo_style_row from GEOG_COORINDATES;

  MI_PRINX MAPINFO_STYLE_ROW
---------- --------------------------------------------------------------------------------
         9 FFG
         8 GGT

Elapsed: 00:00:00.07
satyaki>select * from MAP_INDEX;

  MI_PRINX INDEX_TYPE_ID ORIGINAL_MAP_PUBLICATION_ID ORIGINAL_MAP_SHEET_NUMBER_ID NAME_OF_FEATURE                                                                  MAPINFO_STYLE_ROW                                                                GEOGRAPHICAL_COORDINATES(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), S
---------- ------------- --------------------------- ---------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------
         1            44                        5678                          356 CC                                                                               HHH
         2            55                        6789                          357 BB                                                                               HHH                                                                                                                                                  

Elapsed: 00:00:00.12
satyaki>
satyaki>update VIEW_MI_JOIN_GC
  2    set name_of_feature = 'DD'
  3  where mi_prinx = 1;

1 row updated.

Elapsed: 00:00:00.05
satyaki>
satyaki>select * from VIEW_MI_JOIN_GC;

  MI_PRINX INDEX_TYPE_ID ORIGINAL_MAP_PUBLICATION_ID ORIGINAL_MAP_SHEET_NUMBER_ID NAME_OF_FEATURE                                                                  GEOG_COORDINATES_ID mi_prinx_polygon GEOGRAPHICAL_COORDINATES(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES)
---------- ------------- --------------------------- ---------------------------- -------------------------------------------------------------------------------- ------------------- ---------------- ---------------------------------------------------------------------------------------------------------------
         1            44                        5678                          356 DD                                                                                                 9                9
         2            55                        6789                          357 BB                                                                                                 8                8                                                                                                                 

Elapsed: 00:00:00.08
satyaki>
satyaki>select mi_prinx,mapinfo_style_row from GEOG_COORINDATES;

  MI_PRINX MAPINFO_STYLE_ROW
---------- --------------------------------------------------------------------------------
         9 FFG
         8 GGT

Elapsed: 00:00:00.06
satyaki>
satyaki>update VIEW_MI_JOIN_GC
  2     set mapinfo_style_row = 'OOOO'
  3     where mi_prinx = 1;

1 row updated.

Elapsed: 00:00:00.05
satyaki>
satyaki>select mi_prinx,mapinfo_style_row from GEOG_COORINDATES;

  MI_PRINX MAPINFO_STYLE_ROW
---------- --------------------------------------------------------------------------------
         9 OOOO
         8 GGT

Elapsed: 00:00:00.06
satyaki>
satyaki>select mapinfo_style_row from MAP_INDEX;

MAPINFO_STYLE_ROW
--------------------------------------------------------------------------------
OOOO
HHH

Elapsed: 00:00:00.06
satyaki>

Hope this will help you.

Kind regards.

LOULOU.

Tags: Database

Similar Questions

  • Help me in creating a trigger Insert and Update Options

    Hello

    Please help me in creating a trigger.

    My requirement is that, after insert or update on a Table, I want to launch an event.

    I have started this way, but does not know how to fully implement it.

    I have a dept table


    CREATE TRIGGER DepartmentTrigger
    INSERT AFTER on Dept
    BEGIN
    INSERT INTO mytable VALUES("123","Kiran");
    END DepartmentTrigger;


    Please tell me how I can put the update option also.

    Thanks in advance.

    Please tell me how I can put the update option also.

    Add "or update". ;-)

    Here are a few suggestions, but you definitely need to refer to the manual page that suggested the previous poster.

    CREATE OR REPLACE the TRIGGER DepartmentTrigger
    AFTER INSERT or Update ON Dept
    BEGIN
    INSERT INTO mytable VALUES(:new.) Dept 'DEPT ADDED OR CHANGED');
    END DepartmentTrigger;

    The "or replace" means that you can replace the shutter button as you develop without having to type in a drop statement each time. Just change and reissue your script over and over again until you get it right.

    The addition of "update or" or "Or remove" fact the trigger too much fire for these events. Note, you peut want separate triggers in different scripts and with different names for each event. You must decide whether your design is really the same thing either an insert or an update.

    : new. Dept, this is how you should refer to the vale has changed the Dept column (: old.) Dept is the previous value). I changed the double quotes in the string in the clause VALUES of apostrophes.

    Andy

  • create the user who can update another table schema

    Hello

    We have a system of prod in which we need to update the schema of the application running the declaration different update and create/run function, procedure, package bodies. It is very easy if you use the schema owner. But I need to run the activities of another user due to some restrictions. How can I do?

    Can u suggested to update any table privilege... but this would give all access table dictionary also.

    Is there any privilege that would allow any other user update another schema table without using the schema name before the name of the table?

    PLSS suggest

    John,

    Can we use public synonym for that?  I don't know if the risk of security for her:

    SQL > connect h/h

    Connected.

    SQL > select * from scott.emp;

    Select * from scott.emp

    *

    ERROR on line 1:

    ORA-00942: table or view does not exist

    SQL > connect scott/tiger

    Connected.

    SQL > create public synonym scotemp for scott.emp;

    The synonym is created.

    SQL > connect HR/hr;

    Connected.

    SQL > select * from scotemp;

    Select * from scotemp

    *

    ERROR on line 1:

    ORA-00942: table or view does not exist

    SQL > connect scott/tiger

    Connected.

    SQL > grant select on emp to HR;

    Grant succeeded.

    SQL > connect h/h

    Connected.

    SQL > select * from scotemp;

    ..

    ..

    14 selected lines.

    SQL > show user;

    The USER is 'hr '.

    SQL > update of ename set scotemp = 'SMITHX"where empno = 7369;

    setting a day of scotemp set ename = 'SMITHX"where empno = 7369

    *

    ERROR on line 1:

    ORA-01031: insufficient privileges

    SQL > connect scott/tiger

    Connected.

    SQL > grant update on HR emp;

    Grant succeeded.

    SQL > connect h/h

    Connected.

    SQL > update of ename set scotemp = 'SMITHX"where empno = 7369;

    1 line update.

    SQL > rollback;

    Complete restoration.

    SQL >

    Concerning

    Girish Sharma

  • Instead of Update trigger error

    Hi all
    I tried to solve this problem of course 4-5 days... I'm unable to solve... Can someone please...

    I hhave a requirement I need to update 4 tables from a single form... I created a view that will get the required fields as well as primary keys for all these tables of 4.

    Then I created a trigger Instead of Update as below

    CREATE OR REPLACE TRIGGER "HOMEPAGEVIEW_UPDATE".
    Instead of UPDATE on cts_homepageview
    BEGIN
    DECLARE
    vID NUMBER;
    UPDATE cts_hardware_info
    SET BATCH =: NEW. BATCH,
    COMPUTERNAME =: NEW.COMPUTER_NAME,
    OPERATINGSYSTEM_ID =: NEW. OPERATINGSYSTEM_ID
    where computer_id =: NEW.COMPUTER_ID;

    UPDATE cts_server_administrator
    SET PRIMARY_ADMIN_ID =: NEW. PRIMADMIN_ID,
    SECONDARY_ADMIN_ID =: NEW. SECONDADMIN_ID
    WHERE ID =: NEW.ID;

    UPDATE cts_location_info
    SET BUILDING_ROOM_RACK =: NEW. BUILDING_ROOM_RACK
    WHERE LOCATION_ID =: NEW. LOCATION_ID;

    UPDATE cts_maintenace_info
    SET MAINTENANCE_WINDOW =: NEW. MAINTENANCE_WINDOW
    WHERE MAINTENANCE_ID =: NEW. MAINTENANCE_ID;
    END;


    When I try to update a record, am gettting the following error

    ORA-20505: DML error: p_rowid = 488, p_alt_rowid = COMPUTER_ID, p_rowid2 =, p_alt_rowid2 is. ORA-02014: cannot select the UPDATES view DISTINCT, GROUP BY, etc.
    Error failed to process row in the CTS_HOMEPAGEVIEW table.
    Ok


    I've already posted this question on the forum, but force the answers so try my luck again... Can someone please... I searched the forum for similar requests ths... Found a post the solution bt is not given...


    Thank you
    Yvette

    Published by: user0012 on April 29, 2009 09:54

    Yvette:

    Most likely you typoed a name table or a column in the code in the trigger name. Check that the table names and column names referenced in the trigger are valid. Take a look at the line with

    OPERATING_SYS =: NEW. OPERATING_SYS

    Is this a valid column?

    CITY

  • Unable to create cache with several tables group

    Hello

    I need to create a group of cache with several tables, which are referential to the other.

    There are 2 related tables and table 1 child...
    While trying this thing, it gives me an error:

    8222: multiple parent tables found

    It is not possible to use several tables of root in a single cache group? Is there another way to use it?

    Script, I used is:

    create a cache group asynchronous writethrough TEST. CG1
    of the TEST. ROOT1)
    KEY PRIMARY ID VARCHAR2 (8 BYTES),
    NAME VARCHAR2 (50 BYTE),
    DESCRIPTION VARCHAR2 (255 BYTE),
    POLICYTYPEID VARCHAR2 (7-BYTE)),

    TEST. ROOT2)
    PARAMTYPEID VARCHAR2 (5 BYTES) PRIMARY KEY,
    PARAMETERUSAGE VARCHAR2 (1 BYTE),
    NAME VARCHAR2 (25 BYTE),
    DESCRIPTION VARCHAR2 (255 BYTE)),

    TEST. CHILD1)
    PARAMDETAILID NUMBER (20) PRIMARY KEY,.
    ID VARCHAR2 (8 BYTE),
    PARAMETERUSAGE VARCHAR2 (1 BYTE),
    DISPLAYVALUE VARCHAR2 (255 BYTE),
    OPERATORID VARCHAR2 (5 BYTE),
    VENDORID NUMBER (20).
    FOREIGN KEY REFERENCES TEST. ROOT1 (ID),
    TEST KEY (PARAMETERUSAGE) REFERENCES STRANGERS. ROOT2 (PARAMETERUSAGE));

    You can't have multiple root within a group of cache tables. The requirements for tables in the group a cache are very strict; There must be only one top-level (root table) table and there may possibly be several children tables. Tables of the child should be linked through foreign keys to the root table or a child table above in the hierarchy.

    The solution for your case is among the tables of root and the other root table in a separate cache group and the child table in a cache group. If you do this, you must take care of a few things:

    1. you cannot define foreign keys between the tables of groups of different cache in TimesTen (keys may exist in Oracle) so the application must enforce referential integrity itself for these cases.

    2. If you load data in a cache group (using LOAD the GROUP CACHE or "load we demand") and Timesten will not automatically load the corresponding data in the other group of cache (since he doesn't know the relationship). The application must load the data into the other group of cache explicitly.

    There is no problem with transactional consistency when changes are pushed to Oracle. TimesTen maintains and reinforces the coherence transactional regardless of how the tables are arranged in groups cache correctly.

    Chris

  • Sequence creates random numbers instead of sequence numbers

    Hello

    I created a sequence on a column in the table and then I created a trigger before insert on this table. Here's the script:

    CREATE SEQUENCES SEQ_COL1
    START WITH 1
    MAXVALUE 9999999999999999999999999999
    MINVALUE 1
    NOCYCLE
    NOCACHE
    ALL;

    CREATE OR REPLACE TRIGGER trig_table1
    before inserting
    Table 1 for each line
    Start

    If: new.col1 is null then
    Select seq_col1.nextval in: double new.col1;
    end if;
    end;

    The problem I have is, the number generated for the col1 is random as after 1, it goes to 230 then to 340 etc... instead of being sequential.

    Please let me know if I'm missing something.

    Hello

    Use the ORDER parameter in your create sequence - statement.

    Heike cordially

  • Create the trigger after insert

    Hello

    I would like to ask how to create a trigger after insert on a table. Basically, what I wanted to do are when an insert to table1 it will insert the records to table2. But the problem is that folders that will be inserted in the new tables are entries/fields, who lived in NEW York City (on table of ld)

    after insertion, I wanted to add another field in table2 as status


    create table table1)

    Name varchar2 (55),
    City varchar2 (55)

    );

    create (table2)

    Name varchar2 (55)
    status int (1)
    )

    Hope you could help me.

    Thank you

    Best regards

    antok1015 wrote:
    Hello

    I would like to ask how to create a trigger after insert on a table. Basically, what I wanted to do are when an insert to table1 it will insert the records to table2.

    It's easy...

    SQL> create table table1(
      2    Name varchar2(55),
      3    City varchar2(55)
      4  );
    
    Table created.
    
    SQL>
    SQL> create table table2 (
      2    Name varchar2(55),
      3    status number(1)
      4  );
    
    Table created.
    
    SQL>
    SQL> create or replace trigger trg_tbl1 after insert on table1
      2  for each row
      3  begin
      4    insert into table2 values (:new.name, 1);
      5  end;
      6  /
    
    Trigger created.
    
    SQL> insert into table1 values ('Fred','Bob');
    
    1 row created.
    
    SQL> select * from table2;
    
    NAME                                                        STATUS
    ------------------------------------------------------- ----------
    Fred                                                             1
    
    SQL>
    

    But the problem is that folders that will be inserted in the new tables are entries/fields, who lived in NEW York City (on table of ld)

    after insertion, I wanted to add another field in table2 as status

    This is not sensible. Please explain what you mean.

  • Update target tables...

    Hello

    I'm actually in a difficult situation. I need to update a table with a view. The table has 3 columns (* col1, col2, col3 *) initialized as NULLs in all records, noting that the other columns are given inside... So what I'm basically doing is this:

    I maps the columns to display in the table and click on the table to display the table operator properties, then set the type of loading for UPDATE. Then under a conditional loading in the filter of the target for the update , I have inserted the following condition:

    (INOUTGRP1.*col4 * IS NOT NULL) which shows a successful validation.

    Taking into account the constraints Match is chosen to be NO_CONSTRAINTS...

    After that I click on col1, col2 and col3 respectively to put the Match column when new line Yes...

    Unfortunately, these warnings will appear saying the following:

    + VLD-2753: all mapped attributes are used in corresponding to the criteria of 15_REC.

    It will be a meaningless update action if all attributes are mapped are used for matching. The update statement selects rows that meet the condition of correspondence and be updated with the same values. Specify at least one mapped attribute of my_table to use for updating by setting use update for Matching No.: ( COL1 COL2 COL3). +


    + VLD-2761: impossible to generate the Merge statement.

    MERGE statement cannot be generated because the COL4 column is used for correspondence and updating. A column can be updated in a merge statement. +



    So if anyone has any kind of idea help as soon as possible... Since it is in fact urgent...

    Thanks a lot already recognizing in reading.
    Hossam

    "Merge statement cannot be generated because the COL4 column is used for correspondence and updating. A column cannot be updated in a merge statement.

    You have to column 4, set the Match column when new line no. and update column Yes?

    Concerning
    Nico

  • trigger several tables, removal of the update

    How to translate this to a trigger?
    if  
       host_decomm is set to 'y' on servers 
    then 
       delete from software_lookup 
             where software_lookup.serverid_lookup = server."ID"
    Oracle: 10 x e

    table * "servers" * a columns
    'ID', HOST_DECOMM

    table * "software_lookup" * a columns
    SOFTWARELOOKUP_ID, SERVERID_LOOKUP

    Hello

    This seems good.

    Two small points: I'd
    (1) paragraph between BEGIN and END
    (2) say SHOW ERROR in script

    CREATE OR REPLACE TRIGGER  "SERVERS_DECOMM_T1"
    AFTER
    update on "SERVERS"
    for each row
    begin
            IF  :NEW.host_decomm = 'y'
            then
               delete from software_lookup
                     where serverid_lookup = :NEW.id;
            END IF;
    end;
    /
    SHOW ERROR
    
  • Trigger on a several tables

    IHAVE a requirement like this.but explained in the form of two table emp and Dept.

    I have a table emp table(empno,ename,deptno) and dept (deptno, dname)
    I have a third emp_details (empno, ename, deptno, dname) of table

    I have a procedure that returns the empdetails
    I have a query in th eabove procedure, select * from emp_details; that returns all the details.

    I want to write a trigger of such that,.
    If I insert or update a row in the emp and dept table and then he should get inserted/updated in the table of emp_details also.
    I am able to write triggers ona single table. I can't get the rows inserted or updated when it comes to multiple tables.

    Data:

    create table emp
    (key primary empno number,)
    Ename varchar2 (20).
    DEPTNO number
    )

    create table dept
    (key primary number depno,)
    DNAME varchar2 (20)
    )

    Insert into dept values(10,'hyderabad');
    Insert into dept values(20,'bangalore');

    Insert into emp values(1,'A',10);
    Insert into emp values(2,'B',20);


    CREATE TABLE EMP_DETAILS
    AS
    SELECT E.ENAME, E.EMPNO, E.DEPTNO, D.DNAME
    Of
    EMP E, DEPT. D
    WHERE E.DEPTNO = D.DEPTNO;


    CREATE OR REPLACE PROCEDURE PROC_EMP_DETAILS (P_EMPNO EMP. EMPNO % TYPE OUT, P_ENAME EMP. ENAME OUT TYPE, P_DEPTNO EMP %. DEPTNO % TYPE OUT, P_DNAME MIN. DNAME % OUT TYPE)
    AS
    / * EXAMPLE OF CODE * /.
    BEGIN
    SELECT * FROM EMP_DETAILS;
    END;
    /

    I tried like this

    CREATE OR REPLACE TRIGGER TRIG_EMP_DETAILS
    AFTER INSERT OR UPDATE ON EMP
    FOR EACH LINE
    REFERENCING OLD AS OLD AGAIN AS NEW
    BEGIN
    INSERT INTO EMP_DETAILS VALUES(:NEW.) EMPNO,: NEW. ENAME,: NEW. DEPTNO,?)

    ???? -As a column, we get dept table how to represent that?

    Could you please guide me in this regard?



    Thanks in advance
    KVB

    Trigger on a several tables

    I think that you don't need triggers on several tables: one is enough:

    CREATE OR REPLACE TRIGGER trig_emp_details
      AFTER INSERT OR UPDATE
      ON emp
      FOR EACH ROW
    BEGIN
      INSERT INTO emp_details
        SELECT :new.empno, :new.ename, :new.deptno, dname
          FROM dept
         WHERE deptno = :new.deptno;
    END;
    
  • DB trigger error - update even table in the script of the trigger

    Hi all
    I have a table tab1, whenever any update is done on this table on column col1 and col2 also needs to get the update. (These are related to user forms can be updated only col1 form frontend)
    So I created a trigger as follows.
    ----------------
    CREATE OR REPLACE TRIGGER tri1
    AFTER UPDATE
    ON tab1
    FOR EACH LINE
    WHERE (NEW.col1 = 'YES')
    DECLARE
    PRAGMA AUTONOMOUS_TRANSACTION;
    BEGIN
    Tab1 SET col2 = 1 UPDATE
    WHERE transaction_id =: OLD.transaction_id;
    COMMIT;
    END;
    --------
    When there is no update on this table, I get following error. Please let me know if I'm missing something.
    ORA-00060: Deadlock detected while you wait resource
    ORA-06512: at "user1.tab1", line 5
    ORA-04088: error during execution of trigger 'user1.tab1 '.
    Please let me know if I'm not clear. Thank you

    Published by: DharV on August 23, 2011 05:17

    Your transaction_id is unique?

    If this is the case, then you could perhaps use AFTER UPDATE BEFORE UPDATE:

    CREATE OR REPLACE TRIGGER tri1
    BEFORE UPDATE
    ON tab1
    FOR EACH ROW
    WHEN (NEW.col1 = 'YES')
    BEGIN
       :NEW.col2 := 1;
    END;
    

    If your transaction_id is not unique - if you really want to update more than one line in tab1 - your update cannot update the same line that the trigger has responded, even with the use of an autonomous transaction. Then you could combine the BEFORE UPDATE trigger above with your own AFTER UPDATE, but then add ROWID! =: OLD. ROWID to where clause, but it would be a bad idea because some of your lines will be updated in a single transaction and others in another transaction.

    In general the autonomous transaction is not a great way to solve the problem of changing table within a trigger. I hope that you have a unique transaction_id and can do BEFORE the UPDATE - if it is then a better way is to save the update id in an overall picture in FOR EACH ROW triggers, and only then AFTER an UPDATE fire is NOT "for each line" update the list of saved IDs

  • create the view of several tables of days

    Hello
    I'll have existing oracle db with the daily stat data, tables strangely named data_mon, data_tue, data_wed... For my batch that I intend to use the view with multiple day data, let say for the last 5 days.
    Do not know how better to implement my sql with these suffixes _mon statement to make it more or less dynamic.
    I create also ever seen from several tables, then, probably, that I can do something that will add daily updated data and remove data from 5 days?

    You will appreciate comments on the details and the whole concept. I have prev sql server experience.

    Thank you
    T

    user12943718 wrote:
    Hello
    I'll have existing oracle db with the daily stat data, tables strangely named data_mon, data_tue, data_wed... For my batch that I intend to use the view with multiple day data, let say for the last 5 days.
    Do not know how better to implement my sql with these suffixes _mon statement to make it more or less dynamic.
    I create also ever seen from several tables, then, probably, that I can do something that will add daily updated data and remove data from 5 days?

    You will appreciate comments on the details and the whole concept. I have prev sql server experience.

    Thank you
    T

    Change the data model if you have 1 table for a day.

    You have just a DATA table and a column to indicate the day of the application.

    Then you don't need a view, do not need anything dynamic, do not need to break your head against a wall for a simple query... etc.

  • Creating a combined chronology based on several chronologies in several tables

    Hello
    I need to extract a timeline for a customer based on valid_from and valid_to dates in several tables.
    For example: I have a table named customers with an id, a valid_from a valid_to date and a table named contracts with contrat_name and customer_id and valid_from valid_to:

    CUSTOMERS:
    ID | VALID_FROM | VALID_TO
    1. 01.03.2010 | 01.01.4000

    CONTRACTS:
    CONTRACT_NAME | CUSTOMER_ID | VALID_FROM | VALID_TO
    Contracted | 1. 01.03.2010 | 01.10.2010
    ContractB | 1. 01.10.2010 | 01.01.4000

    The following statement would now give me the correct chronology:
    Select customer cus.id, con.contract_name, greatest(cus.valid_from,con.valid_from) least(cus.valid_to,con.valid_to) valid_to valid_from
    customers cus
    stupid contracts on cus.id = con.customer_id inner join;

    CUSTOMER | CONTRACT | VALID_FROM | VALID_TO
    1. Contracted | 01.03.2010 | 01.10.2010
    1. ContractB | 01.10.2010 | 01.01.4000

    It works, but I get a problem whenever I have a point from the time there is no contract for a client, but I would always like to have these periods in my calendar:

    Suppose the following data and the same select statement:

    CUSTOMERS:
    ID | VALID_FROM | VALID_TO
    1. 01.03.2010 | 01.01.4000

    CONTRACTS:
    CONTRACT_NAME | CUSTOMER_ID | VALID_FROM | VALID_TO
    Contracted | 1. 01.05.2010 | 01.10.2010
    ContractB | 1. 01.12.2010 | 01.03.2011

    What I would now get would be:
    CUSTOMER | CONTRACT | VALID_FROM | VALID_TO
    1. Contracted | 01.05.2010 | 01.10.2010
    1. ContractB | 01.12.2010 | 01.03.2011

    But what I get is the following:
    CUSTOMER | CONTRACT | VALID_FROM | VALID_TO
    1. null | 01.03.2010 | 01.05.2010
    1. Contracted | 01.05.2010 | 01.10.2010
    1. null | 01.10.2010 | 01.12.2010
    1. ContractB | 01.12.2010 | 01.03.2011
    1. null | 01.03.2011 | 01.01.4000

    What I won't is generate a result with contract = null any time, there is no contract because I actually want to join the chronology of several different tables into one and it would be very complicated to assume things based on what data can or can not be found in a specific table.

    Thanks for any help or ideas,
    Kind regards
    Thomas

    Hi, Thomas,.

    Whenever you have a problem, after the sample data in a form that people can use to recreate the problem and test their ideas. CREATE TABLE and INSERT statements are great.
    For example:

    CREATE TABLE     customers
    (     customer_id     NUMBER (6)     PRIMARY KEY
    ,     valid_from     DATE          NOT NULL
    ,     valid_to     DATE          NOT NULL
    );
    
    INSERT INTO customers (customer_id, valid_from, valid_to) VALUES (1, DATE '2010-03-01', DATE '4000-01-01');
    INSERT INTO customers (customer_id, valid_from, valid_to) VALUES (2, DATE '2010-03-01', DATE '4000-01-01');
    
    CREATE TABLE     contracts
    (     contract_name     VARCHAR2 (15)     NOT NULL
    ,     customer_id     NUMBER (6)
    ,     valid_from     DATE          NOT NULL
    ,     valid_to     DATE          NOT NULL
    );
    
    INSERT INTO contracts (contract_name, customer_id, valid_from,        valid_to)
         VALUES            ('Contract 1a', 1,           DATE '2010-03-01', DATE '2010-10-01');
    INSERT INTO contracts (contract_name, customer_id, valid_from,        valid_to)
         VALUES            ('Contract 1b', 1,           DATE '2010-10-01', DATE '4000-01-01');
    
    INSERT INTO contracts (contract_name, customer_id, valid_from,        valid_to)
         VALUES            ('Contract 2a', 2,           DATE '2010-05-01', DATE '2010-10-01');
    INSERT INTO contracts (contract_name, customer_id, valid_from,        valid_to)
         VALUES            ('Contract 2b', 2,           DATE '2010-12-01', DATE '2011-03-01');
    

    If a customer contracts of n, then you might need as much as 2n + lines 1 output for this client:
    (a) 1 row showing the period, not from the date of customers both valid_from 1st contract begins
    (b) n lines indicating the periods under contracts
    (c) n lines indicating the period right after each contract, until the following starts (or valid_to of the client, if there is no next contract)
    However, you will have lines for periods not contacted ((a) or (c)) if their mandate is 0 days.

    This sounds like a job for the UNION. Make a UNION 3 lanes in a subquery to generate (a), (b), and (c), and then put a WHERE clause on the combined, results to filter the lines not length 0.
    In the query subsidiary UNION, (a) can be done with a query GROUP BY, and (c) can be done using the analytical function of LEAD.

    WITH     union_data     AS
    (
         SELECT       ua.customer_id
         ,       NULL               AS contract_name
         ,       MIN (ua.valid_from)     AS valid_from
         ,       MIN (oa.valid_from)     AS valid_to
         FROM       customers     ua
         JOIN       contracts     oa     ON     ua.customer_id     = oa.customer_id
         GROUP BY  ua.customer_id
         --
        UNION ALL
         --
         SELECT       customer_id
         ,       contract_name
         ,       valid_from
         ,       valid_to
         FROM       contracts
         --
        UNION ALL
         --
         SELECT       uc.customer_id
         ,       NULL               AS contract_name
         ,       oc.valid_to          AS valid_from
         ,       LEAD ( oc.valid_from
                     , 1
                     , uc.valid_to
                     ) OVER ( PARTITION BY  uc.customer_id
                        ORDER BY      oc.valid_from
                         )          AS valid_to
         FROM       customers     uc
         JOIN       contracts     oc     ON     uc.customer_id     = oc.customer_id
    )
    SELECT       *
    FROM       union_data
    WHERE       contract_name     IS NOT NULL
    OR       valid_from     < valid_to
    ORDER BY  customer_id
    ,       valid_from
    ;
    

    Output:

    CUSTOMER_ID CONTRACT_NAME   VALID_FROM VALID_TO
    ----------- --------------- ---------- ----------
              1 Contract 1a     01.03.2010 01.10.2010
              1 Contract 1b     01.10.2010 01.01.4000
    
              2                 01.03.2010 01.05.2010
              2 Contract 2a     01.05.2010 01.10.2010
              2                 01.10.2010 01.12.2010
              2 Contract 2b     01.12.2010 01.03.2011
              2                 01.03.2011 01.01.4000
    

    Published by: Frank Kulash, April 20, 2011 06:51
    Examples added

  • Need to create a trigger that generates multiple rows in another table

    {size: 12} Hello

    I use Oracle9i (9.2.0.4.0)

    My problem is:
    I need to create a trigger that create different in the other table lines when someone will introduce a new line in the first table.

    For this, I use the table 4:

    1. first one where you want the trigger.
    2. a second just to take information.
    3. a third to make a trip meter.
    4. the fourth is the table where I want to create new lines. {size}

    Here is a summary of these 4 tables:

    {color: blue} Table 1
    STOJOU
    Number ¿Nulo? Tipo
    STOFCY_0 NOT NULL VARCHAR2 (9)
    ITMREF_0 NOT NULL VARCHAR2 (60)
    LOT_0 NOT NULL VARCHAR2 (45)
    VCRTYP_0 NOT NULL NUMBER (3)
    VCRNUM_0 NOT NULL VARCHAR2 (45)
    VCRLIN_0 NOT NULL NUMBER (10)
    QTYSTU_0 NOT NULL NUMBER
    NUMVCR_0 NOT NULL VARCHAR2 (45) {color}

    {color: blue} Table 2
    ITMMASTER
    Number ¿Nulo? Tipo
    ITMREF_0 NOT NULL VARCHAR2 (60)
    ZCOEFI_0 NOT NULL NUMBER {color}

    {color: blue} Table 3
    ZCTUART;
    Number ¿Nulo? Tipo
    CTUART_0 NOT NULL NUMBER (10)
    CTUDATE_0 NON NULL DATE {color}

    {color: blue} Table 4
    ZUART
    Number ¿Nulo? Tipo
    ZUARTDM_0 NOT NULL VARCHAR2 (102)
    STOFCY_0 NOT NULL VARCHAR2 (9)
    STOCOU_0 NOT NULL NUMBER
    ITMREF_0 NOT NULL VARCHAR2 (60)
    LOT_0 NOT NULL VARCHAR2 (45)
    CREDAT_0 NOT NULL DATE
    QTYSTU_0 NOT NULL NUMBER
    STA_0 NOT NULL VARCHAR2 (3)
    VCRLIN_0 NOT NULL NUMBER (10)
    VCRNUM_0 NOT NULL VARCHAR2 (45)
    VCRTYP_0 NOT NULL NUMBER (3) {color}

    I do this trigger:
    CREATE OR REPLACE TRIGGER CREA_REGISTROS_TRAZA
    AFTER INSERT ON STOJOU
    FOR EACH ROW
    DECLARE
       n INTEGER;
       ct NUMBER;
       fecha_actual VARCHAR2;
       fecha_old VARCHAR2;
       codigo_dm VARCHAR2;
    BEGIN
       fecha_actual := TO_CHAR(SYSDATE, 'DD/MM/YYYY');
       -- SELECT TO_CHAR(sysdate, 'DD/MM/YYYY') INTO fecha_actual FROM DUAL;
       SELECT TRUNC((STOJOU.QTYSTU_0/ITMMASTER.ZCOEFI_0),0) INTO n FROM STOJOU INNER JOIN ITMMASTER ON STOJOU.ITMREF = ITMMASTER.ITMREF;
          FOR i IN 1 .. n
       LOOP
          SELECT CTUART_0,TO_CHAR(CTUDAT_0, 'DD/MM/YYYY') INTO ct,fecha_old FROM ZCTUART;
          IF fecha_old <> fecha_actual THEN
            ct := 0;
          END IF;
          ct := ct + 1;
          INSERT INTO ZCTUART
            (CTUART_0,CTUDAT_0)
          VALUES
            (ct,SYSDATE);
          codigo_dm := SUBSTR('000000000000000000000',1,20 - LENGHT(NEW.ITMREF_0)) || NEW.ITMREF_0 || SUBSTR('0000000000',1,10 - LENGHT(NEW.LOT)) || NEW.LOT || SUBSTR('0000',1,4 - LENGHT(ct)) || ct;
          INSERT INTO ZUART
            (ZUARTDM_0,ITMREF_0,CREDAT_0,STA_0,QTYSTU_0,LOT_0,STOFCY_0,VCRLIN_0,VCRNUM_0,VCRTYP_0)
          VALUES
            (codigo_dm,NEW.ITMREF_0,SYSDATE,0,NEW.QTYSTU_0,NEW.LOT_0,NEW.STOFCY_0,NEW.VCRLIN_0,NEW.VCRNUM_0,NEW.VCRTYP_0);
       END LOOP;
    END CREA_REGISTROS_TRAZA;
    /
    {size: 12} And the error message I get States is just:
    "The trigger was created with compilation errors."
    Thanks for the help {size}

    You must prefix your NEW "columns" with a colon, as in: NEW.

  • Update of a Base Table in a view with a UNPIVOT function.

    Hello
    I have a requirement to upgrade a Base Table in a view.
    This view has the query by using a function of the UNPIVOT operator to display the columns in the tables of the basic of the lines.
    I need to update/insert in / remove the Base Table by accessing the view (the user has access to the Base Table, so the DML on the view).

    Here is the table I created:-
    CREATE TABLE PERSON_DETAILS
    (
      PID            VARCHAR2(10 BYTE),
      FIRSTNAME      VARCHAR2(1000 BYTE),
      LASTNAME       VARCHAR2(1000 BYTE),
      PHONENUMBER    VARCHAR2(1000 BYTE),
      ADDRESS1       VARCHAR2(1000 BYTE),
      ADDRESS2       VARCHAR2(1000 BYTE),
      COUNTRY_CODE   VARCHAR2(1000 BYTE),
      LANGUAGE_CODE  VARCHAR2(1000 BYTE),
      EMAIL          VARCHAR2(1000 BYTE)
    )
    The sample values are inserted in this table using the script below: -.
    insert into person_details values ('1','XYZ','ABC','1234567890','India','Asia','IN','EN','[email protected]');
    insert into person_details values ('2','XYZ2','ABC2','1234567890','India','Asia','IN','EN','[email protected]');
    The code for the display is as below: -.
    CREATE OR REPLACE FORCE VIEW PERSON_DETAILS_VIEW
    (
       PID,
       CD_NAME,
       CD_VALUE
    )
    AS
       SELECT "PID", "CD_NAME", "CD_VALUE"
         FROM person_details UNPIVOT INCLUDE NULLS (cd_value
                             FOR cd_name
                             IN  (firstname AS 'First Name',
                                 lastname AS 'Last Name',
                                 phonenumber AS 'Phonenumber',
                                 address1 AS 'address1',
                                 address2 AS 'address2',
                                 country_code AS 'Country Code',
                                 language_code AS 'Language Code',
                                 email AS 'Email') );
    Here are the values of the view:-
    PID CD_NAME         CD_VALUE
    1    First Name       XYZ
    1    Last Name       ABC
    1    Phonenumber  1234567890 
    1    address1         India
    1    address2         Asia
    1    Country Code   IN
    1    Language Code EN
    1    Email               [email protected]
    2    First Name       XYZ2
    2    Last Name       ABC2
    2    Phonenumber  1234567890
    2    address1         India
    2    address2         Asia  
    2    Country Code   IN
    2    Language Code EN
    2    Email               [email protected]
    The user would fire any statement as below: -.
    update person_details_view 
    set cd_value = 'US' where CD_NAME = 'IN'
    The statement above should update the PERSON_DETAILS database table.

    I understand that I can write an INSTEAD OF trigger, but I don't know what logic to write in the trigger so that the condition is met.

    My Version of Oracle
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
    PL/SQL Release 11.1.0.7.0 - Production
    CORE    11.1.0.7.0    Production
    TNS for Solaris: Version 11.1.0.7.0 - Production
    NLSRTL Version 11.1.0.7.0 - Production
    Any help would be much appreciated.

    Thank you
    Ankit Khare.

    Published by: Ankit_Khare84 on June 28, 2012 14:47

    It is definitely possible with an INSTEAD of trigger.

    for example:

    create or replace
    TRIGGER ioft_person
    INSTEAD OF UPDATE
    ON person_details_view
    FOR EACH ROW
    declare
    firstname1  person_details.firstname%type;
    BEGIN
    
                  SELECT firstname_new into firstname1
                  FROM   (SELECT pid, cd_name, cd_value
                          FROM   
    
                              (
                                  select * from person_details_view where (pid, cd_name) not in (select :new.pid, :new.cd_name from dual)
                                  union all
                                  select :new.pid, :new.cd_name, :new.cd_value from dual
                              )
    
                          )
                  PIVOT  ( max(cd_value) AS new FOR (cd_name) IN
                                                          ('First Name' AS firstname,
                                                            'Last Name' as lastname,
                                                            'Phonenumber' as phonenumber,
                                                            'address1' as address1,
                                                            'address2' AS address2,
                                                            'Country Code' as country_code,
                                                            'Language Code' as language_code,
                                                            'Email' as email
                                                            )
                  )  where pid = :old.pid;
    
      UPDATE person_details
      SET firstname = firstname1
      WHERE pid = :old.pid;
    END ioft_role_perm;
    

    and that run

    update person_details_view
    set cd_value = 'X|X' where cd_name = 'First Name' and pid=1
    

    The logic is: you need to convert back the view through swivel

Maybe you are looking for