Before Insert TRIGGER to create partitions problem

Hello

I m having a problem with the following situation in Oracle 8i:

I have a table TEST_TABLE, which is divided by the beach with a DATE column. The idea is to have a partition for each month, so the HIGH_VALUE of partitions is always the first day of the month following that represents the partition.

I created a BEFORE TRIGGER INSERT on the table TEST_TABLE, which tests if the partition for the month of registration which is being inserted exists and, in case it doesn´t, a PROC AUTONOMOUS_TRANSACTION is called to create the TRIGGER.

Running the code below one can see that even if partitions are created as expected, when you try to insert a record with a date greater than the last partition for the first time, this error is returned:
ORA-14400: inserted partition key exceeds plu legal partition key.

Note that if you run the same statement again insert, it s inserted correctly on the partition that was created the first try.

I´ll appreciate any help on this matter.

code
----------------
CREATE TABLE TEST_TABLE)
IDENTIFICATION NUMBER,
DATE OF THE DT
)
TABLESPACE USERS
PARTITION BY RANGE (DT)
(
PART_B42009 PARTITION VALUES LESS THAN (TO_DATE ('2009-01-01 00:00:00 ',' YYYY-MM-DD HH24:MI:SS ',' NLS_CALENDAR = GREGORIAN '))
LOGGING
TABLESPACE USERS
);
/

CREATE OR REPLACE PROCEDURE SP_ADD_PARTITION (TEST_TABLE P_DATE. DT % TYPE)
IS
PRAGMA AUTONOMOUS_TRANSACTION;
V_STR VARCHAR2 (500);
BEGIN

V_STR: = 'ALTER TABLE TEST_TABLE ADD.
|| 'PARTITION BIRD | TO_CHAR ("P_DATE, ' YYYYMM")
|| ' VALUES LESS (TO_DATE ("')).
|| TO_CHAR (ADD_MONTHS (P_DATE, 1), "YYYY-MM"). '-01 00:00:00 ','
|| ((' SYYYY-MM-DD HH24:MI:SS "," NLS_CALENDAR = GREGORIAN "))';

EXECUTE IMMEDIATE (V_STR);

END SP_ADD_PARTITION;
/

CREATE OR REPLACE TRIGGER TR_B_I_R_TEST_TABLE
BEFORE INSERTING
ON TEST_TABLE FOR EACH LINE
DECLARE
NUMBER OF V_PARTITION_EXISTS;
BEGIN

IF: NEW. DT > = TO_DATE ('2009-01-01 00:00:00 ',' ' YYYY-MM-DD HH24:MI:SS) THEN

IMMEDIATELY EXECUTE (' SELECT COUNT (1) ")
|| "Of all_tab_partitions atp'."
|| "WHERE atp.table_name ="table_test"'.
|| "AND the atp. Nom_partition =: v1 ')
IN V_PARTITION_EXISTS
WITH THE HELP OF "BIRD" | TO_CHAR(:NEW.) "DT,"YYYYMM";)

IF V_PARTITION_EXISTS = 0 THEN

DBMS_OUTPUT. Put_line ('Partition [' |]) 'BIRD ' | TO_CHAR(:NEW.) "DT,"YYYYMM"). does not exist!') ;
DBMS_OUTPUT. Put_line ('creation..');
SP_ADD_PARTITION (: NEW.) DT);
DBMS_OUTPUT. Put_line ('success.');

IMMEDIATELY EXECUTE (' SELECT COUNT (1) ")
|| "Of all_tab_partitions atp'."
|| "WHERE atp.table_name ="table_test"'.
|| "AND the atp. Nom_partition =: v1 ')
IN V_PARTITION_EXISTS
WITH THE HELP OF "BIRD" | TO_CHAR(:NEW.) "DT,"YYYYMM";)

IF V_PARTITION_EXISTS = 1 THEN
DBMS_OUTPUT. Put_line ("it s visible at this point..");
ON THE OTHER
DBMS_OUTPUT. Put_line ("it s not visible at this point..");
END IF;

ON THE OTHER

DBMS_OUTPUT. Put_line ('Partition [' |]) 'BIRD ' | TO_CHAR(:NEW.) DT, "YYYYMM")
|| already exists! ") ;

END IF;

END IF;

DBMS_OUTPUT. Put_line ('continues with insertion...");

END TR_B_I_R_TEST_TABLE;
/

-Go to the low score
INSERT INTO TABLE_TEST VALUES (1, TO_DATE ('2008-12-31 23:59:59 ',' YYYY-MM-DD HH24:MI:SS'));))

-Returns the error on the first try
INSERT INTO TABLE_TEST VALUES (2, TO_DATE ('2009-01-01 00:00:01 ',' YYYY-MM-DD HH24:MI:SS'));))

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

It is the use of the pragma AUTONOMOUS TRANSACTION. Your current transaction cannot see the result of this DOF since it occurs outside of the current transaction. The clue is in the name.

Of course, you cannot run the DDL in a trigger without use of this pragma, so you're pretty much stuck. There is a solution in 11g, but that will not help you. Unfortunately, your only option is to pre-create the partitions required in front of the need. For example, you might have a DBMS JOB to create a partition for the next month, which takes place the last day of each month (or logical date of company).

Cheers, APC

blog: http://radiofreetooting.blogspot.com

Tags: Database

Similar Questions

  • Redirect data to another table using before insert trigger.

    Dear all,
    How can I redirect the data to another table in a before Insert trigger? My database is Oracle10g.
    I have a table EMP (EMP_ID, LAST_NAME, SALARY).
    I have another EMP_COPY table with the same structure. I also have a before Insert trigger on the EMP table.
    Based on a condition that I have to redirect the data in table EMP_COPY. Let's say the condition is EMP_ID = 100.
    I fire an insert on EMP table for example INSERT IN EMP(EMP_ID,LAST_NAME,SALARY) VALUES(100,'Dev',500).
    On the inside of the front Insert trigger on the EMP table, I have the code
    IF EMP_ID = 100 THEN
    INSERT INTO EMP_COPY (EMP_ID, LAST_NAME, SALARY)
    VALUES(:NEW.) EMP_ID,: NEW. LAST_NAME,: NEW. SALARY);
    COMMIT;
    ON THE OTHER
    NULL;
    END IF;
    But the problem here is that data goes to EMP table of origin also although I don't want. He should do an Insert into EMP table only if EMP_ID! = 100.

    One way has been to raise a user-defined exception inside the If statement and not handle it so that the original insert on table EMP fails but INSERT comes in the EMP_COPY table. But in this solution since the inside the trigger unhandled exception, it propagates to the calling environment. And I can't stand outside relaxation as the calling environment is a form of Oracle Apps standard that cannot be customized.

    Any kind of help will be highly appreciated that I am fighting for more than two weeks.

    Thanks in advance
    Dev

    Remove the autonomous transaction pragma... and then try again.

  • Prior insertion trigger problem

    Hi Guyz,

    I am facing a strange problem or may be wrong somewhere, but can't find my problem, I hope I'll get the solution to my problem here im im.

    IM updating my quantities in the table MN_ITEM_DETAILS.
    SQL> DESC MN_ITEM_DETAILS
     Name                                      Null?    Type
     ----------------------------------------- -------- ------------------
     SI_SHORT_CODE                                      NUMBER(10)
     SI_CODE                                            NUMBER(15)
     ITEM_DESCP                                         VARCHAR2(200)
     ITEM_U_M                                           VARCHAR2(6)
     ITEM_QTY                                           NUMBER(10)
     ITEM_REMARKS                                       VARCHAR2(100)
    I have below master-details on before insert trigger block works very well.
    SQL> DESC MN_MDV_MASTER
     Name                                      Null?    Type
     ----------------------------------------- -------- ----------------------------
     MDV_NO                                    NOT NULL NUMBER(15)
     MDV_DATE                                           DATE
     WHSE_LOC                                           VARCHAR2(15)
     PROJ_WHSE                                          VARCHAR2(30)
     ACTIVITY_LOC                                       VARCHAR2(30)
     MRF_NO                                             VARCHAR2(30)
     CLIENT                                             VARCHAR2(30)
     CLIENT_PO#                                         VARCHAR2(15)
     CLIENT_PO_DATE                                     DATE
     WHSE_INCHG                                         VARCHAR2(30)
     WHSE_DATE                                          DATE
     RECD_BY                                            VARCHAR2(30)
     INSPECTED_BY                                       VARCHAR2(30)
     DRIVER_NAME                                        VARCHAR2(30)
     REMARKS                                            VARCHAR2(200)
     RECD_BY_DATE                                       DATE
     INSPECTED_DATE                                     DATE
     DRIVER_NAME_DATE                                   DATE
     CST_CENTER                                         VARCHAR2(15)
    
    SQL> DESC MN_MDV_DETAILS
     Name                                      Null?    Type
     ----------------------------------------- -------- ----------------------------
     MDV_NO                                             NUMBER(15)
     ITEM_CODE                                          NUMBER(15)
     ITEM_DESCP                                         VARCHAR2(150)
     ITEM_U_M                                           VARCHAR2(6)
     ITEM_QTY                                           NUMBER(6)
     ITEM_BALANCE                                       NUMBER(10)
     PROJECT                                            VARCHAR2(15)
     ACTIVITY                                           VARCHAR2(15)
     LOCATION                                           VARCHAR2(15)
    All the triggers to INSERT before & after INSERTION block level
    PRE-INSERT -- ON details block level 
    UPDATE MN_ITEM_DETAILS
    SET ITEM_QTY = NVL(ITEM_QTY,0) - NVL(:MN_MDV_DETAILS.ITEM_QTY,0)
    WHERE SI_CODE = :MN_MDV_DETAILS.ITEM_CODE;
    
    POST-INSERT MASTER BLOCK LEVEL TRIGGER
    INSERT INTO MN_MRBV_MASTER(
    MDV#        ,
    MDV_DATE      , 
    WHSE_LOC      , 
    CST_CENTER    )VALUES
    (:MN_MDV_MASTER.MDV_NO         ,        
    :MN_MDV_MASTER.MDV_DATE       ,        
    :MN_MDV_MASTER.WHSE_LOC       ,        
    :MN_MDV_MASTER.CST_CENTER); 
    
    POST-INSERT ON DETAILS BLOCK LEVEL
    INSERT INTO MN_MRBV_DETAILS(
    MDV#         ,
    ITEM_CODE     , 
    ITEM_DESCP    , 
    ITEM_U_M           , 
    QTY      , 
    ITEM_BALANCE  ,
    PROJECT       , 
    ACTIVITY      , 
    LOCATION      )VALUES
    (:MN_MDV_DETAILS.MDV_NO         ,
    :MN_MDV_DETAILS.ITEM_CODE     , 
    :MN_MDV_DETAILS.ITEM_DESCP     ,
    :MN_MDV_DETAILS.ITEM_U_M       ,
    :MN_MDV_DETAILS.ITEM_QTY       ,
    :MN_MDV_DETAILS.ITEM_BALANCE,
    :MN_MDV_DETAILS.PROJECT      ,  
    :MN_MDV_DETAILS.ACTIVITY      , 
    :MN_MDV_DETAILS.LOCATION       ); 
    until the above works fine and its update of the MN_ITEM_DETAILS. ITEM_QTY correctly

    but im using the same as above in the MASTER-DETAIL table below but do not update the ITEM_QTY in MN_ITEM_DETAILS
    SQL> DESC MN_MRBV_MASTER
     Name                                      Null?    Type
     ----------------------------------------- -------- ----------------------------
     MDV#                                      NOT NULL NUMBER(15)
     MDV_DATE                                           DATE
     WHSE_LOC                                           VARCHAR2(15)
     RET_FRM_PROJECT                                    VARCHAR2(1)
     RET_FRM_CLIENT                                     VARCHAR2(1)
     CST_CENTER                                         VARCHAR2(15)
     WHSE_INCHG                                         VARCHAR2(30)
     WHSE_DATE                                          DATE
     RETURN_BY                                          VARCHAR2(30)
     INSPECTED_BY                                       VARCHAR2(30)
     RETURN_BY_DATE                                     DATE
     INSPECTED_BY_DATE                                  DATE
     DRIVER_NAME                                        VARCHAR2(30)
     DRIVER_DATE                                        DATE
     REMARKS                                            VARCHAR2(250)
    
    SQL> DESC MN_MRBV_DETAILS
     Name                                      Null?    Type
     ----------------------------------------- -------- ----------------------------
     MDV#                                               NUMBER(15)
     ITEM_CODE                                          NUMBER(15)
     ITEM_DESCP                                         VARCHAR2(150)
     ITEM_U_M                                           VARCHAR2(6)
     QTY                                                NUMBER(6)
     ITEM_BALANCE                                       NUMBER(10)
     PROJECT                                            VARCHAR2(15)
     ACTIVITY                                           VARCHAR2(15)
     LOCATION                                           VARCHAR2(15)
    PRE-INSERT--> here its not updating the MN_ITEM_DETAILS.ITEM_QTY table any sugesstion plz why its not updating...?
    MDV_DETAILS
    UPDATE MN_ITEM_DETAILS
    SET ITEM_QTY = NVL(ITEM_QTY,0) + NVL(:MN_MRBV_DETAILS.QTY,0)
    WHERE SI_CODE = :MN_MRBV_DETAILS.ITEM_CODE;
    Concerning

    Houda

    Published by: houda Shareef on January 8, 2011 02:19

    try to write your code in before update trigger

  • TRIGGER BEFORE INSERT OR UPDATE OR DELETE

    I want to create a single trigger before each insert or update or delete, a record gets copied into the AUDIT table. works very well on the insertion and to day but you have problem clear.

    Can you please help me debug this error. The problem of mutation.

    create or replace

    BIU_FS_Test_T RELAXATION

    BEFORE INSERT OR UPDATE OR DELETE

    ON FS_Test

    REFERRING AGAIN AS NINE OLD AND OLD

    FOR EACH LINE

    DECLARE

    number of voffender_history_id;

    Start

    IF the INSERTION

    SELECT FS_Test_history_seq. nextval in the double voffender_history_id;

    INSERT INTO FS_Test_history (OFFENDER_HISTORY_ID,

    OFFENDER_ID, FIRST_NAME, MIDDLE_NAME, LAST_NAME.

    SX, SKIN_TONE, HEIGHT, WEIGHT, RACE, EYE_COLOR,

    HAIR_COLOR, BLOOD_TYPE, VARIOUS, DATE_OF_BIRTH.

    PLACE_OF_BIRTH, TRANSACTION)

    VALUES (voffender_history_id,

    : new. OFFENDER_ID,: new. First name: new. MIDDLE_NAME,: new. LAST_NAME,

    : new. RACE: new. SX,: new. SKIN_TONE,: new. HEIGHT: new. WEIGHT: new. EYE_COLOR,

    : new. HAIR_COLOR,: new. BLOOD_TYPE,: new. VARIOUS: new. DATE_OF_BIRTH,

    :new. PLACE_OF_BIRTH,

    "INSERT");

    ELSIF UPDATE

    et (

    NVL (:OLD. First name, 'x') <> nvl():NEW. First name, 'x') or

    NVL (:OLD. MIDDLE_NAME, 'x') <> nvl():NEW. MIDDLE_NAME, 'x') or

    NVL (:OLD. Last_name, 'x') <> nvl():NEW. Last_name, 'x') or

    NVL (:OLD. RACE 0) <> nvl():NEW. RACE 0) or

    NVL (:OLD. SX 0) nvl(:NEW <>.) SX 0) or

    NVL (:OLD. SKIN_TONE 0) <> nvl():NEW. SKIN_TONE 0) or

    NVL (:OLD. HEIGHT 0) <> nvl():NEW. HEIGHT 0) or

    NVL (:OLD. WEIGHT 0) <> nvl():NEW. WEIGHT 0) or

    NVL (:OLD. EYE_COLOR 0) <> nvl():NEW. EYE_COLOR 0) or

    NVL (:OLD. HAIR_COLOR 0) <> nvl():NEW. HAIR_COLOR 0) or

    NVL (:OLD. BLOOD_TYPE 0) <> nvl():NEW. BLOOD_TYPE 0) or

    NVL (:OLD. VARIOUS, ' x') <> nvl():NEW. VARIOUS, ' x') or

    NVL (:OLD. DATE_OF_BIRTH, January 1, 1900 ") <> nvl():NEW. " DATE_OF_BIRTH, January 1, 1900 ") or"

    NVL (:OLD. PLACE_OF_BIRTH 0) <> nvl():NEW. PLACE_OF_BIRTH 0)

    )

    THEN

    SELECT FS_Test_history_seq. nextval in the double voffender_history_id;

    INSERT INTO FS_Test_history (OFFENDER_HISTORY_ID,

    OFFENDER_ID, FIRST_NAME, MIDDLE_NAME, LAST_NAME.

    SX, SKIN_TONE, HEIGHT, WEIGHT, RACE, EYE_COLOR,

    HAIR_COLOR, BLOOD_TYPE, VARIOUS, DATE_OF_BIRTH.

    PLACE_OF_BIRTH, TRANSACTION)

    VALUES (voffender_history_id,

    : new. OFFENDER_ID,: new. First name: new. MIDDLE_NAME,: new. LAST_NAME,

    : new. RACE: new. SX,: new. SKIN_TONE,: new. HEIGHT: new. WEIGHT: new. EYE_COLOR,

    : new. HAIR_COLOR,: new. BLOOD_TYPE,: new. VARIOUS: new. DATE_OF_BIRTH,

    :new. PLACE_OF_BIRTH, "UPDATE");

    ELSIF DELETION THEN

    SELECT FS_Test_history_seq. nextval in the double voffender_history_id;

    SELECT FS_Test_history_seq. nextval in the double voffender_history_id;

    INSERT INTO FS_Test_history (OFFENDER_HISTORY_ID,

    OFFENDER_ID, FIRST_NAME, MIDDLE_NAME, LAST_NAME.

    SX, SKIN_TONE, HEIGHT, WEIGHT, RACE, EYE_COLOR,

    HAIR_COLOR, BLOOD_TYPE, VARIOUS, DATE_OF_BIRTH.

    PLACE_OF_BIRTH, TRANSACTION)

    VALUES (voffender_history_id,

    : new. OFFENDER_ID,: new. First name: new. MIDDLE_NAME,: new. LAST_NAME,

    : new. RACE: new. SX,: new. SKIN_TONE,: new. HEIGHT: new. WEIGHT: new. EYE_COLOR,

    : new. HAIR_COLOR,: new. BLOOD_TYPE,: new. VARIOUS: new. DATE_OF_BIRTH,

    :new. PLACE_OF_BIRTH, 'CLEAR');

    END IF;

    end;

    Error I get

    Error from the 1 in the command line:

    Remove from the fs_test where PK = 41476

    Error report:

    SQL error: ORA-04091: table get OUT. SOR_O_HISTORY is changing, function of triggering/can not see

    ORA-06512: at «GET. » BIU_FS_TEST_T', line 111

    ORA-04088: error during execution of trigger ' EB. BIU_FS_TEST_T'

    04091 00000 - «table %s. » "%s is changing, function of triggering/can not see.

    * Cause: A trigger (or plsql user-defined function that is referenced in)

    ( this statement) attempted to watch (or modify) a table that has been

    in the middle to be modified by the statement that shot.

    * Action: Rewrite the trigger (or function) so it does not read this table.

    PS 11g r2

    Thank you.

    n_shah18-

    Is there a parent-child relationship between the tables involved in the triggering event? Is there a foreign key used above the child table that references the parent table?

    If you have a parent-child relationship and a trigger on the parent table must be running some DML on the child table, and, if the child table as a foreign key (FK) back to the parent table, any DML on the child table will cause a recursive SQL statement to check the constraint. Indirectly, this will cause an exception table mutation.

    So watch the dependencies on SOR_O_HISTORY / FS_TEST_HISTORY/all other related table. A FOREIGN KEY can make it to mutate (no pun intended).

    Hope this will help you find the root cause.

  • Trigger before insert

    Hi, I need help to create a trigger before the insertion of the STA_STARTDATE column in the employees table
    CREATE OR REPLACE TRIGGER trig_start_date
    BEFORE INSERT OF STAFF FOR EACH ROW
    BEGIN
      IF STA_STARTDATE < '01-JAN-1980'
        THEN
            RAISE_APPLICATION_ERROR(-20900, 'No earlier than 1/1/1980');
      END IF;
    END;
    Note: If the declaration is < 01 - JAN-1980 quotes

    When I run the script, it comes up with this error:

    Error report:
    ORA-04073: invalid column list for this type of trigger
    04073 00000 - "list of invalid columns for this type of trigger.
    * Cause: A list of columns has been specified for a non updated trigger type.
    * Action: Delete the column list.

    * fixed by replacing the OF keyword with, but still have a warning

    Published by: 888948 on October 2, 2011 16:48

    Hello

    to use a column in a trigger, the column must have: new for new values, or: old old values before the name of the column, so your if statement should begin with:

    IF :NEW.STA_STARTDATE 
    

    Herald tiomela
    http://htendam.WordPress.com

  • after insert trigger problem

    I am trying to write a trigger that will update a neck date after each insertion.

    CREATE OR REPLACE TRIGGER PS1. XX_CDATE
    AFTER INSERT ON ps1.COMMNPLANBUDGET
    Old SEO AS OLD AS new NEW
    FOR EACH LINE
    declare the PRAGMA AUTONOMOUS_TRANSACTION;
    BEGIN
    Update ps1.COMMNPLANBUDGET set CREATION_DATE = sysdate where PROJECTID =: new. PROJECTID;
    commit;
    END XX_CDATE;

    For the same thing, I wrote the code above, it created successfully but when I am inserting a record pass date is not updated. Please let me know where I did wrong. Thanks in advance

    It's everything you want

    create or replace trigger ps1.xx_cdate before insert on ps1.commnplanbudget referencing old as old new as new for each row
    begin
         :new.creation_date := sysdate;
    end xx_cdate;
    

    Use a trigger to insert BEFORE and just change the: NEW value

  • I'm having a problem reinstalling Windows XP. After formatting the disk and create partitions, it will not complete the installation.

    Problem reinstalling Windows XP

    I'm having a problem reinstalling Windows XP.  After formatting the disk and create partitions, it will not complete the installation.  He goes back and reinstall Windows again.  I go through the installation of 39 minutes, the computer restarts to complete and it loops back and start the installation again. Why it does this and what I can do to move beyond this?

    Go into the BIOS and make sure that you your hard drive is not set to off, and that is the first priority boot CD and your second HD.

    Don't push when it restarts anything just to let him do what he does and report back if anything I'm sorry I have no idea.

  • How do validation before inserting a line when press a button insert create?

    Hi all;

    I use JDev 11.1.1.2.0

    How do for validation before inserting a line when press on create a button insert into ADF11g?

    < af:commandButton actionListener = "#{bindings." CreateInsert.execute}.
    Text = "CreateInsert."
    Disabled = "#{!}" bindings. CreateInsert.enabled}.
    ID = "cb8" / >

    best regards;

    Hello
    You can do per set actionlister with commit and action with createinsert
    Good luck
    lixinzhu

  • Help me. Will be the Trigger below cause performance problem?

    Experts,

    I'm Mohanasundar, senior Software engineer, Antares limited systems. We use oracle for our product. 16 years one of our capture of audit trail table (* LOGTABLE *) do not have a primary key column. If we write at the same time more than one journal to LOGTABLE, the column was even, due to which order of this column wasn't properly.

    To avoid this, we have added the trigger of bellows for LOGTABLE.

    CREATE OR REPLACE TRIGGER TR_PK_ATTACH_TBL+.
    BEFORE INSERT ON ATTACH_TABLE FOR EACH LINE.
    DECLARE A1 NUMBER +
    START to+.
    * SELECT LOGTABLE_SEQ. NEXTVAL IN A1 FROM DUAL; *
    *: NEW.LT_INDEX: = A1; *
    END; +

    LT_INDEX Here are the newly added primary key column.
    LOGTABLE_SEQ is an oracle sequence.

    This table contains about 10 million documents.
    Initially we updated the sequence for the LT_INDEX column to existing records.

    Details of our server:

    OS: windows Server2008 R2
    Web server: Apache Tomcat 6.35
    Oracle: 11G

    The problem is,

    "* We made this change to the production for the last version (this month)." "Our teams COMPUTER, reports that after this change, the site is too slow (Server Web is in place, but the answer is too slow)."

    Will this trigger slow the performance of oracle db? *

    Please help us, our eight month project is literally dependent on this key.

    Thank you
    Mohanasundar.n

    Relaxation itself I do not introduce overhead costs have a significant impact on performance, and I can see why you would use an approach to relaxation based during the redevelopment of this on an existing system.
    Could really do with some stats here, an AWR report on a busy to begin with.

    But I'll take a guess - could be contention on the right-hand side of the index, a hot block as all these inserts are consecutive values are inserted.
    A reverse key index could (maybe with the emphasis on the word!) help here.

  • Splitting words before inserting into the new table

    SQL> desc company
     Name                                      Null?    Type
     ----------------------------------------- -------- ----------------------------
     CO_ID                                     NOT NULL NUMBER(9)
     R_DATE                                             TIMESTAMP(0)
     CONAME                                             VARCHAR2(140)
     ESTYR                                              NUMBER(5)
     COUNTRY                                            VARCHAR2(32)
     PRODUCTS                                           VARCHAR2(3900)
    
    SQL> select unique(products) from company where rownum<6;
    
    PRODUCTS
    --------------------------------------------------------------------------------
    bars,tubes,pre-weld seams,isosceles and un-isosceles angles,special profiles,u-p
    rofiles,plates,t-profiles,flanges,tube fittings,round,hollow bars,double-t-profi
    les (ipe und ipb),pipe clamps,flat,special designs according to drawings for new
     products,profiles for floor covering,hexagon
    
    carbon steel bright bars,tool steel bright bars,alloy steel bright bars,stainles
    s steel bright bars
    
    cold rolled coil,e.g sheet,prepainted sheet,galvalume coil,copper scrap,stainles
    s steel sheet,shredded scrap,hms1 and 2,cobalt ore,rail scrap,hot rolled coil,ga
    lvanized sheet,iron ore
    
    PRODUCTS
    --------------------------------------------------------------------------------
    
    ferro alloy producer
    switch technology,special tubes,rails,steel strip,special steel
    I created another table:
    crate table keywords(
    k_id number(14),
    keywords varchar2(500) unique
    );
    
    create sequence keywords_pk_seq
    start with 3484 increment by 1;
    
    create or replace trigger keywords_pk_trig
    before insert on keywords
    referencing new as new
    for each row
    begin
    select keywords_pk_seq.nextval into :new.k_id
    from dual;
    end;
    /
    I want to:
    one) to split columns of company.products where is comma (,) for example, bars, tubes, pre-soudures, tubes will became 4 different as words:
    bars
    tubes
    welds
    tubes

    (b) then I want to insert these words in the table keywords.keywords with condition separated by ignoring the existing keywords.keywords (e.g. tubes came twice, let's get just a tubes) for example
    k_id keywords
    1 bars
    2 tubes
    3 before welding

    Thank you and best regards

    You can use the merge statement:

    for example

    merge into keywords
    using (
    with t as (select 'pipes,taps,spanner,spade,tubes' as txt from dual)
    -- end of sample data
      select distinct REGEXP_SUBSTR (txt, '[^,]+', 1, level) kw
      from t
      connect by level <= length(regexp_replace(txt,'[^,]*'))+1
      ) x
     on (keywords.keyword = x.kw)
    when not matched then insert (id, keyword) values (key_seq.nextval, x.kw)
    /
    

    But keep in mind that the sequence in this example will be updated for each line independently to know if she gets inserted or not.
    If you set the ID using a sequence triggered when inserting then this will solve this problem.

  • After insert trigger mutation of error, what is the way to overcome it.

    I have two tables namely profiles_answers and user_profileanswers. Based on the requirement that is: when a user inserts the answer in the table of user_profileanswers I need to calculate the weight-age of this issue with the many options available in the profiles_answers table and update this table user_profileanswers age-weight. So, for that, I wrote the following after trigger insert. But when I try to insert it throws me error mutation. As I update the table, used to insert action in the trigger. Please let me how can know I solve this problem.

    create or replace

    AI_weightageCaluculation TRIGGER AFTER

    FOR EACH row INSERT on user_profileanswers

    BEGIN

    DECLARE

    v_a VARCHAR2 (50);

    YaeUb VARCHAR2 (50);

    V_c VARCHAR2 (50);

    v_d VARCHAR2 (50);

    ve VARCHAR2 (50);

    a_weightage NUMBER;

    b_weightage NUMBER;

    c_weightage NUMBER;

    d_weightage NUMBER;

    e_weightage NUMBER;

    BEGIN

    SELECT option_a, option_b, option_c, option_d, option_e IN

    v_a, YaeUb, v_c, v_d, profiles_answers FROM ve

    WHERE profile_questions_id =: new.profilequestion_id;

    IF (v_a IS NOT NULL AND YaeUb IS NOT NULL AND v_c IS NOT NULL AND v_d IS NOT NULL AND EV IS NOT NULL) THEN

    BEGIN

    a_weightage: = 85;

    b_weightage: = 60;

    c_weightage: = 45;

    d_weightage: = 30;

    e_weightage: = 15;

    END;

    ELSIF (v_a IS NOT NULL AND YaeUb IS NOT NULL AND v_c IS NOT NULL AND v_d IS NOT NULL AND ve IS NULL) THEN

    BEGIN

    a_weightage: = 85;

    b_weightage: = 60;

    c_weightage: = 30;

    d_weightage: = 15;

    END;

    ELSIF (v_a IS NOT NULL AND YaeUb IS NOT NULL AND v_c IS NOT NULL AND v_d IS NULL AND ve IS NULL) THEN

    BEGIN

    a_weightage: = 85;

    b_weightage: = 45;

    c_weightage: = 15;

    END;

    ON THE OTHER

    BEGIN

    a_weightage: = 85;

    b_weightage: = 15;

    END;

    END IF;

    IF: new.answer = 'A' THEN

    BEGIN

    UPDATE user_profileanswers

    SET weightage = a_weightage

    WHERE user_id =: new.user_id

    AND profileanswer_id =: new.profileanswer_id

    AND profilequestion_id =: new.profilequestion_id;

    END;

    ELSIF: new.answer = 'B' THEN

    BEGIN

    UPDATE user_profileanswers

    SET weightage = b_weightage

    WHERE user_id =: new.user_id

    AND profileanswer_id =: new.profileanswer_id

    AND profilequestion_id =: new.profilequestion_id;

    END;

    ELSIF: new.answer = 'C' THEN

    BEGIN

    UPDATE user_profileanswers

    SET weightage = c_weightage

    WHERE user_id =: new.user_id

    AND profileanswer_id =: new.profileanswer_id

    AND profilequestion_id =: new.profilequestion_id;

    END;

    ELSIF: new.answer = ' THEN

    BEGIN

    UPDATE user_profileanswers

    SET weightage = d_weightage

    WHERE user_id =: new.user_id

    AND profileanswer_id =: new.profileanswer_id

    AND profilequestion_id =: new.profilequestion_id;

    END;

    ON THE OTHER

    BEGIN

    UPDATE user_profileanswers

    SET weightage = e_weightage

    WHERE user_id =: new.user_id

    AND profileanswer_id =: new.profileanswer_id

    AND profilequestion_id =: new.profilequestion_id;

    END;

    END IF;

    END;

    END;

    Thanks in advance.

    Hmm... Why do after insertion?

    CREATE OR REPLACE TRIGGER BI_weightageCaluculation

    FRONT INSERT ON user_profileanswers FOR EACH ROW

    BEGIN

    SELECT BOX: NEW. RESPONSE

    WHEN 'A' a_weight THEN

    WHEN 'B' THEN b_weight

    WHEN 'C' THEN c_weight

    When ' THEN d_weight

    Of OTHER e_weight

    END

    in: new.weightage

    (SELECT 85 a_weight

    Case option_result

    WHEN 31 THEN 60

    WHEN 15 THEN 60

    WHEN 7 THEN 45

    another 15

    end b_weight

    Case option_result

    WHEN 31 THEN 45

    WHEN 15, THEN 30

    When 7 then 15

    END c_weight

    Case option_result

    WHEN 31 THEN 30

    When 15 then 15

    END d_weight

    Case option_result

    WHEN 31 THEN 15

    END e_weight

    FROM (SELECT DECODE (option_a, NULL, 0, 1) +)

    Decode (option_b, NULL, 0, 2) +.

    Decode (option_c, NULL, 0, 4) +.

    Decode (option_d, NULL, 0, 8) +.

    Decode(option_e,,0,16) option_result

    OF profiles_answers

    WHERE profile_questions_id =: new.profilequestion_id));

    END;

    /

    HTH

  • Truncate a field before inserting

    Suppose I have the following text:

    Array: mytable

    field: username (VARCHAR2 (35))

    field: zip (VARCHAR2 (5))

    I am importing data from an excel with columns username and zip file. A couple of the usernames contains more than 35 characters, and some postal codes are in the format xxxx-xxxxx, therefore containing more than 5 characters.

    I tried to write an insert before truncating the data until they are inserted. For any user name that is longer than 35 characters in Excel, simply truncate the username to the first of 35 characters. Similarly, for any ZIP code which is above 5 characters, just truncate the zip on the first 5 characters.

    The trigger for postal codes:

    CREATE OR REPLACE

    Trigger_zipc RELAXATION

    BEFORE INSERTING

    ON mytable

    FOR EACH LINE

    BEGIN

    SELECT substr (: new .zip, 1, 5)

    IN: new.zip

    Double;

    END;

    However, I get the error: ORA-12899: value too large for column

    I understand that this happens because the value is read before my validation, and because the value is too large, it is not allowed to insert.

    I'm not sure where to go from here. Any suggestions?

    
    

    Is it excel file or a csv file.

    I think you can look into creating an external table as well.

    Before inserting it just to make a substring.

    Something along the lines.

    CREATE TABLE ext_data (
     username CHAR(500),
      zip CHAR(500))
      ORGANIZATION EXTERNAL (TYPE ORACLE_LOADER DEFAULT DIRECTORY ext_tab_dir
      ACCESS PARAMETERS (FIELDS TERMINATED BY ',')
      LOCATION ('csv.dat'));
    
    insert into mytable
    select substr(username,1,35) , substr(zip,1,5) from ext_data;
    

    I hope this helps.

    Alvinder

  • How to do before delete trigger

    Hi all
    I want to do before you remove the trigger
    I have 2 tables hr_api_transactions and new_table
    When I delete records from table hr_api_transations
    This deleted record should then be stored / table new_table insert
    for this I need before delete trigger

    How can I do?

    Also, you do not want the table name must be specified

    CREATE OR REPLACE TRIGGER before_delete_trigger
        BEFORE DELETE
            ON HR_API_TRANSACTIONS
            FOR EACH ROW
    BEGIN
      delete from new_table
       where new_table.name = :OLD.api_addtnl_info;
    END;
    

    Justin

  • After the insert trigger error

    Hello

    I created a trigger after insert according to for me, it's OK, but it show me error can not insert Null in code.
     
    
    
    CREATE OR REPLACE TRIGGER  "AFT_INS_CRM_CUSTOMER_CONTACTS" 
      AFTER INSERT ON FH_TAPROD.CRM_SALES_CUSTOMER_CONTACTS 
    FOR EACH ROW
    DECLARE
    custo_id NUMBER;
    var_code varchar2(8);
    cont_code varchar2(5);
    BEGIN
    
    SELECT CUSTOMER_ID INTO custo_id FROM CRM_SALES_CUSTOMER_CONTACTS WHERE ID =:NEW.ID;
    
      select CODE into var_code from VENDOR_CUSTOMER_MAS where CRM_CUST_ID=custo_id ;
    
      SELECT LPAD ( NVL(MAX(CONTACT_CODE), 0)+ 1, 5, '0') into cont_code FROM vendor_customer_contact
      
      insert into VENDOR_CUSTOMER_CONTACT(SBU_CODE ,CODE,CONTACT_CODE, CONTACT_NAME ,PHONE_NO1 ,MOBILE_NO ,EMAIL, ADDRESS1,CUST_ID,UPLOAD_CRM) 
       values('0002',var_code,cont_code,:NEW.CONTACT_NAME,:NEW.CONTACT_PHONE,:NEW.CONTACT_CELL,:NEW.CONTACT_EMAIL,:NEW.CONTACT_ADDRESS,custo_id,'CRM');
    
    
    END; 
    CREATE TABLE  "CRM_SALES_CUSTOMER_CONTACTS" 
       (     "ID" NUMBER, 
         "CUSTOMER_ID" NUMBER, 
         "CONTACT_NAME" VARCHAR2(255), 
         "CONTACT_EMAIL" VARCHAR2(255), 
         "CONTACT_PHONE" VARCHAR2(255), 
         "CONTACT_CELL" VARCHAR2(255), 
         "CONTACT_ADDRESS" VARCHAR2(4000), 
          PRIMARY KEY ("ID") ENABLE
       )
    /
    INSERT INTO CRM_SALES_CUSTOMER_CONTACTS VALUES (204414862717175278810736770347803084847, 204232590877603599356756434170666837075, 'SANJAY',    '[email protected]', 1246456, 57152357,'Near Post Office');
    CREATE OR REPLACE TRIGGER  "BIU_CRM_SALES_CUST_CONTACTS" 
    BEFORE INSERT OR UPDATE ON CRM_SALES_CUSTOMER_CONTACTS
        FOR EACH ROW
    begin
        if inserting and :new.id is null then
            select to_number(sys_guid(),'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') into :new.id from dual;
        end if;
        if inserting then
            :new.created_by := nvl(v('APP_USER'),USER);
            :new.created_on := sysdate;
        end if;
        if updating then
            :new.updated_by := nvl(v('APP_USER'),USER);
            :new.updated_on := sysdate;
        end if;
    end;
    My after insert a table
    CREATE TABLE  "VENDOR_CUSTOMER_CONTACT" 
       (     "SBU_CODE" VARCHAR2(4) NOT NULL ENABLE, 
         "CODE" VARCHAR2(8) NOT NULL ENABLE, 
         "CONTACT_CODE" VARCHAR2(5) NOT NULL ENABLE, 
         "CONTACT_NAME" VARCHAR2(1000) NOT NULL ENABLE, 
         "PHONE_NO1" VARCHAR2(100), 
         "MOBILE_NO" VARCHAR2(25), 
         "EMAIL" VARCHAR2(1000), 
         "ADDRESS1" VARCHAR2(4000), 
         "CUST_ID" NUMBER, 
         "UPLOAD_CRM" VARCHAR2(10), 
          CONSTRAINT "VCV_PK" PRIMARY KEY ("SBU_CODE", "CODE", "CONTACT_CODE") ENABLE
       )
    ORA-01400: cannot insert NULL into ("FH_TAPROD"."VENDOR_CUSTOMER_CONTACT"."CODE") ORA-06512: at "FH_TAPROD.AFT_INS_CRM_CUSTOMER_CONTACTS", line 30 ORA-04088: error during execution of trigger 'FH_TAPROD.AFT_INS_CRM_CUSTOMER_CONTACTS'
    How to solve it.

    Thank you

    Published by: 805629 on January 14, 2011 01:26

    This is the error...

    SELECT CUSTOMER_ID INTO custo_id FROM CRM_SALES_CUSTOMER_CONTACTS WHERE ID =: NEW.ID;

    Is there a reason why not to use: NEW. CUSTOMER_ID rather the query?

  • Helps relaxation that fits into another table before insertion

    I have two tables:

    TABLE A as follows:
    x, y, z
    1, 1, 1
    1, 1, 2
    1, 1, 3

    TABLE B as follows
    c, d, e, f
    1, 1, 1, 8
    1, 1, 1, 2
    1, 1, 1, 6
    1, 1, 2, 5
    1, 1, 2, 10
    1, 1, 2, 3
    1, 1, 3, 12
    1, 1, 3, 11
    1, 1, 3, 15


    I want a trigger, BEFORE insertion in the TABLE B, I want to INSERT into TABLE A as follows:

    c = > x
    d = > y
    e = > z

    ONLY IF a (x, y, z) record does not already exist in the TABLE.

    That is to say. If record (1, 1, 3) already exists in the TABLE, we do not try to put it back when you insert in the TABLE B with values containing (1, 1, 3).

    You will need

    CREATE OR REPLACE trig_on_insert
    BEFORE INSERT ON B
    FOR EACH ROW
    IS
       lv_count    NUMBER := 0;
    BEGIN
       SELECT COUNT(1)
         INTO lv_count
         FROM a
        WHERE x = :new.c
          AND y = :new.d
          AND z = :new.e ;
    
       IF ( lv_count = 0 ) THEN
          INSERT INTO A VALUES(:new.c,:new.d ,:new.e);
       END IF;
    END;
    /
    

    Concerning
    Arun

Maybe you are looking for

  • How to connect signals Ni SCB - 264 X

    We bought PXI switch 2532 with TB-2641. We also havebought burst of SCB-264 X. I have a question about the signalconnections. SCB-264 X is supplied with only the connections of signals (C0, C1etc). But what of the ground for each signal. How to conne

  • error code 10 to create folder

    I develop a vi in which I have to create a folder in any drive (for example, D:\dataacquisition) in the acquisition of data folder, I have to create the folder date and name of the folder and we must create files of information of the patient for exa

  • How to recover data from a deleted partition?

    Original title: Hi,. I have compaq 420 im using windows professional help, but I m scaning my disc hrad but 1 partion office is deleted please retrieve my data and pxe error not cooneted hard drive is not dected in my laptop

  • Import a database

    Oracle SQL Developer Data Modeler. Version 3.3.0.747I'm trying to import a databaae into the Data Modeler.It seems stuck.The design of generation of guest does not progress.So far it has been in the design of generation BOF more than 24 hours.There i

  • Query does not not in shell scripts

    Hi Experts,I created a query to check the temp tablespace use in production servers and I wanted to run the query using shell scripts, but when I run the query in the script I get no error or output I am unale to correct where exactly, I make the mis