Help on Database Trigger

Hi Users,
I have a requirement like whenever there is a insert or update on a table, some of the column values have to be inserted or updated into another table.
There is a table reporter_status with the following column. All are varchar2 data type
Source Table Name : reporter_status
Node             summary               mtr
1.1.1.1          Interface 9 xxx        66
1.2.3.4          Interface 10  xxx     03
10.10.1.2       Interface 11 xxx
2.2.2.2          Interface 5  xxx       90Target Table Name : interface_downtime
Node          Port           mtrDuring Insert:
If any new row is inserted into the above table and if the value of mtr is not null
1) extract only the numeric portion of the summary column i:e 9,10,11 and insert the above values into another table "interface_downtime" along with node
and mtr.
During Update :
When there is an update to mtr column the interface_downtime.mtr column should be summed based on the current node
I have written a trigger but it doesn't seem to work. I have no idea where I am wrong.
Your kind help is required.
My trigger Body:
CREATE OR REPLACE TRIGGER REPORTER.trigger_update_mtr
AFTER INSERT OR UPDATE
ON REPORTER.REPORTER_STATUS
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
WHEN (old.mtr is not null)
DECLARE
tmpPort  varchar2(3);
BEGIN
  IF INSERTING
  THEN
      tmpPort := substr(:old.summary,11,1);
      INSERT INTO interface_downtime values (:old.node,tmpPort,:old.mtr);
  END IF;    
  IF UPDATING
  THEN
            UPDATE interface_downtime
            SET       MTR   =  MTR + :OLD.MTR
            WHERE  NODE  = :old.node;
   END IF;
   EXCEPTION
     WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
       RAISE;
END;
/

Change the trigger as below. The issue is that you are confused in old and new values. Its working fine as per my testing
CREATE OR REPLACE TRIGGER trigger_update_mtr
after INSERT OR UPDATE
ON REPORTER_STATUS
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
WHEN (new.mtr is not null)
DECLARe
tmpPort  varchar2(3);
BEGIN
    IF INSERTING
  THEN
        tmpPort := substr(:new.summary,11,1);
      INSERT INTO interface_downtime values (:new.node,tmpPort,:new.mtr);
  END IF;    
  IF UPDATING
  THEN
              UPDATE interface_downtime
            SET       MTR   =  MTR + :new.MTR
            WHERE  NODE  = :old.node;
   END IF;
   EXCEPTION
     WHEN OTHERS THEN
null;
END;

Similar Messages

  • How to get SSO logged user information in a database trigger

    my database is 9i
    my application server is 9i too
    I need to track which SSO user is inserting data into a table , so how can i get the information of that user in a database trigger on that table
    thanks

    Hi,
    The only way I could figure out how to get it was to import the com.sap.security.api.
    1. From Window>Preferences>Java-->Classpath Variables, set up a variable called WEBAS_HOME pointing to:
    <drive>/usr/sap/<SID>/jc00/j2ee/cluster/server0/
    2. Right click on properties of your Dynpro Project->Properties->Java Build Path->Libraries and click "Add Variable." 
    3. Select WEBAS_HOME and click "Extend"
    4. Select /bin/ext/com.sap.api.sda/com.sap.security.api.jar and click OK.
    If you don't have access to a portal directory, you will have to get this jar file and copy it to your workspace to add to your build path instead of the above steps.
    Then in your code:
    import com.sap.security.api.IUser;
    import com.sap.security.api.IUserAccount;
      public void GetLogonID( )
         String LogonID;
         try {
              /*     create an user object from the current user */
              IWDClientUser wdUser = WDClientUser.getCurrentUser();
              IUser user = wdUser.getSAPUser();
              if (user != null) {
                   IUserAccount acct = user.getUserAccounts()[0];
                   if (acct != null) {
                        LogonID = acct.getLogonUid();
                   } else
                        LogonID = "acct null";
              } else {
                   LogonID = "user null";
              wdContext.currentContextElement().setDisplayName(user.getFirstName() + " " + user.getLastName());
              wdContext.currentContextElement().setLogonID(LogonID.toUpperCase());
         } catch (Exception e) {
              e.printStackTrace();
    Hope this helps...  I had to search several places before I found all the necessary stuff.  Maybe I can get my first points!
    Thanks,
    Andrew

  • PROBLEM WITH A DATABASE TRIGGER

    WHEN A USER ENTERS A STATUS CODE BETWEEN 70 AND 86 ON THE PAB_SUSP TABLE, THE COMPLETION DATE FIELD ON THE PAB_SUSP TABLE GETS UPDATED WITH THE SYSTEM DATE WHICH IS FINE. THE
    COMPLETION DATE FIELD ON THE EREC_SUSP TABLE FOR THE SAME CORRESPONDING RECORD SHOULD ALSO GET UPDATED WITH THE SAME SYSTEM DATE. SOMETIMES THE COMPLETION DATE FIELD ON THE EREC_SUSP TABLE GETS UPDATED CORRECTLY WITH THE SAME SYSTEM DATE. OTHER TIMES IT DOES NOT. WHAT IS THE PROBLEM?
    I AM ENCLOSING THE CODE IN THE DATABASE TRIGGER. NOTE ALSO THAT THE SUSP_DT FIELD ALSO GETS UPDATED.
    DECLARE
    cursor get_user is SELECT user_id, FROM oracle_user
    WHERE username = USER;
    user_number NUMBER(10,0);
    num_recs NUMBER(10,0);
    num_rec_recs2 NUMBER(10,0);
    seq number(10,0);
    ssn VARCHAR2(9);
    nm VARCHAR2(27);
    dob DATE;
    pg VARCHAR2(2);
    rank VARCHAR2(2);
    act_cd VARCHAR2(3);
    clk NUMBER(10,0);
    recv_dt DATE;
    susp_dt DATE;
    cmpl_dt DATE;
    ocll VARCHAR2(7);
    tagd VARCHAR2(7);
    chg_ind VARCHAR@(1);
    BEGIN
    open get_user;
    fetch get_user into user_number;
    close get_user;
    IF (:new.lcl_pab_susp_stat_cd <> :old.lcl_pab_susp_stat_cd AND
    :new.lcl_pab_susp_stat_cd >= '70' AND
    :new.lcl_pab_susp_stat_cd <= '86') THEN
    cmpl_dt := sysdate;
    END IF;
    IF :new.lcl_susp_dt <> :old.lcl_susp_dt THEN
    chg_ind := 'Y';
    susp_dt := :new.lcl_susp_dt;
    else
    susp_dt := :old.lcl_susp_dt;
    END IF;
    seq := :new.pab_susp_seq;
    ssn := :new.ind_ssn;
    nm := :new.ind_nm;
    dob := :new.ind_dob;
    pg := :new.ind_pg_cd;
    rank := :new.mil_rank_cd;
    act_cd := :new.lcl_act_cd;
    clk := :new.lcl_act_clk_no;
    recv_dt := :new.lcl_recv_dt;
    ocll := :new.ocll_id;
    tagd := :new.tagd_id;
    SELECT COUNT(*) INTO num_recs2
    FROM EREC_SUSP
    WHERE SUSP_SEQ = :old.susp_seq;
    IF num_recs2 > 0 THEN
    UPDATE erec_susp
    SET pab_susp_seq = seq,
    ind_ssn = ssn,
    ind_nm = nm,
    ind_dob = dob,
    ind_pg_cd = pg,
    mil_rank_cd = rank,
    erec_ofc_act_cd = act_cd,
    erec_of_clk_no = clk,
    lcl_cmpl_dt = cmpl_dt,
    lcl_recv_dt = recv_dt,
    lcl_susp_dt = susp_dt,
    ocll_id = ocll,
    tagd_id = tagd,
    lcl_chg_ind = chg_ind,
    rec_tx_dt = SYSDATE,
    rec_tx_user_no = user_number
    where susp_seq = new.susp_seq;
    END IF;
    END;

    S. Wolicki, Oracle wrote:
    No, you cannot!!! NLS_CHARACTERSET is not a session parameter in Oracle.
    Assuming IBM MessageBroker connects through OCI, setting the NLS_LANG variable in its environment to .EE8ISO8859P2 could help. You should also review MessageBroker's documentation, especially any configuration parameters for Oracle, to see if the character set is not configurable there. There is nothing to be done on the Oracle side.
    By the way, can you name any particular characters that do not show up correctly and tell me what you see in place of them?
    -- SergiuszHi Sergiusz,
    I replace back and forth the accented characters, but for large queries, it takes a very long time:
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(c,'ő','&#x0151'),
    'Ő','&#x0150'),'ű','&#x0171'),'Ű','&#x0170'),'é','&#x00E9'),'É','&#x00C9'),'á','&#x00E1;'),'Á','&#x00C1'),
    'ú','&#x00FA'),'Ú','&#x00DA'),'ö','&#x00F6'),'Ö','&#x00D6'),'ü','&#x00FC'),
    'Ü','&#x00DC'),'ó','&#x00F3'),'Ó','&#x00C3'),'í','&#x00ED'),'Í','&#x00CD');
    (The semicolons let down for display.)I thought that I would change the nls_lang on the client side in a Java node but it is not sure that will work.
    Thank you for your answer.
    Sada
    Edited by: 1002028 on 2013.04.24. 13:48
    Edited by: 1002028 on 2013.04.24. 13:49

  • Passing variables to a procedure inside a database trigger

    Hi,   I am new to oracle Forms . I am having a table in database . For that table i had written a database trigger which will fire after insert or update . I am using a procedure inside the Trigger . I want to pass arguments to the procedure which is being called in the trigger . The variables which i want to pass to the procedure are one is a primary key and the other is a normal cloumn which are in the same table for which the trigger is written. Can anybody help me regarding this.?

    > But when i m calling it with forms i m getting an error
    You cannot call a database trigger from forms. A trigger is part of the table, e.g.:
    create trigger my_trigger
    before insert or update or delete on my_table
    for each row

  • Forms update vs. database trigger

    Hi,
    I learn oracle forms using sample schema HR.
    This schema has a trigger "update_job_history" that fires AFTER UPDATE OF "job_id", "department_id" ON "employees"....
    Now,( in forms) when I want to update just employee's salary trigger fires.
    Forms obviously take all items as being updated as I read from the beginning of error message:
    UPDATE EMPLOYEES SET EMPLOYEE_ID=:1,FIRST_NAME=:2,LAST_NAME=:3,EMAIL=:4,PHONE_NUMBER=:5,HIRE_DATE=:6,JOB_ID=:7,SALARY=:8,COMMISSION_PCT=:9,MANAGER_ID=:10,DEPARTMENT_ID=:11 WHERE ROWID=:12 etc.
    (This error appears only If I update the same employee the second time - that's when a unique key in job_history table gets violated...)
    My question is How can I update only certain items so the triggers fires only when I do update "job_id" or "department_id"
    Is it an issue I should take care of in -pre/on-update triggers?
    Thanks in advance for any suggestion on how to go about this.

    Hello,
    Yah, this will surely help. Sorry for my previous post. I was thinking in terms of PL/SQL, but just consults the forms and it a should work fine.
    Set Update Changed Columns Only to Yes in the following circumstances:
    To save on network traffic, if you know an operator will primarily update only one or two columns.
    To avoid re-sending large items that are not updated, such as images or LONGs.
    To fire database triggers on changed columns only. For example, if you implement a security scheme with a database trigger that fires when a column has been updated and writes the userid of the person performing the update to a table.

  • After Logon on Database Trigger Not Working From Client Terminal

    Hi Every One
    I Have a Problem, I'am Using Oracle 10g R2, I'd Written After Logon on Database Trigger, I'd Written The Trigger Under The Under The User With DBA Privileges, and it is work Fine, but it is work only when i Logon On The Database from The Server Terminal with any user, and If Logon From any Other Terminal It Is Not Work,
    Can any One Know The Reason, Please Help me
    Yasser Mokhtar

    Please post the trigger code.

  • Need to call an application with in  Database trigger

    Dear all
    I have created a database trigger that reads the new values inserted on my table and append the record on a text file on the operationg system.
    I have a software that convert this text file to binary file.
    I can call this software using a command prompt
    I need to issue this command prompt from within the database trigger
    I need to Know is there a package or plsql code that can call an application.
    We can do this easly on the form builder using the comman HOST
    But I need to issue this command from within SQL PLUS
    is there a way
    please help
    thanks in advance

    another simple way is to write a C program with an exec function that calls the external program.
    The C function can be called by the Oracle server as an extproc (like a PL/SQL funtion call).
    Check for the extproc Oracle documentation:
    http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b15658/ldr_demo.htm
    Sample programs you will find in your $ORACLE_HOME/plsql/demo directory.
    Kind regards
    Karsten

  • How to send error message to forms from Database Trigger

    Hi, Please help me to send error message to forms from Database Trigger?
    RgDs,
    Madesh.R.M

    You are correct, the On-Error trigger is a Forms trigger. However, if your Form is going to display the error generated by the database stored procedure or trigger - you might not see the database error in your Form unless you check the DBMS_ERROR_CODE in the On-Error trigger and manually display the Error Code and associated Text. I've see this happen with a co-worker. The Form she was working on was based on a table with an Before-Insert trigger. Because she was not explicitely handling the error from the Before-Insert trigger in the Forms On-Error trigger, her Form appeared to halt for no reason at all. Once she added code to the On-Error trigger in the Form to handle the DBMS_ERROR_CODE, she discovered the trigger was producing an error and was able to show the error to the user in the On-Error trigger.
    I understand the desire to keep as much as possbile in the database, but with that comes some extra coding in your Forms to handle this. This extra coding could easily be placed in a Forms Library, attached to a Form and called in the On-Error trigger. Your code could look like this:
    DECLARE
       /*This example assumes you have an Alert defined
          in your Form called: 'ERROR' */  
       al_id    ALERT;
       al_text  VARCHAR2(200);  /* Max text of a Forms Alert message*/
       al_btn   NUMBER;
    BEGIN
    IF DBMS_ERROR_CODE != 0 THEN
       /* Error code is ORA-00000 Normal Successful completion
           So only handle non-zero errors  */
       al_text := DBMS_ERROR_CODE||':'||DBMS_ERROR_TEXT;
       al_id := Find_Alert('ERROR');
       set_alert_property(al_id, alert_message_text, al_text);
       al_btn := show_alert(al_id);
    END IF;
    END;Your original question was "How to send error message to forms from Database Trigger?" The answer is you don't because Forms already gets the database error code and database message through the Forms DBMS_ERROR_CODE and DBMS_ERROR_TEXT functions. Look these up in the Forms help and it should clear things up for you.
    Craig...
    Edited by: CraigB on Jun 14, 2010 4:49 PM
    Edited by: CraigB on Jun 14, 2010 4:49 PM
    Edited by: CraigB on Jun 14, 2010 4:50 PM
    Edited by: CraigB on Jun 14, 2010 4:51 PM
    Edited by: CraigB on Jun 14, 2010 4:51 PM

  • Form Navigation problem while firing Database trigger

    Hi,
    A Database trigger is made to fire when a field is updated via forms and the trigger will give an error if the user is not authorized to modify the data. Everything is working fine and the trigger gives an error as palnned. After the error is thrown, we are unable to navigate between the forms and in addition we are uanble to perform any sort of actions like querying, deleting and cancelling. On Occasion we get errors like I/O buffer error and the errors keep on changing for every update. Plz help me to resolve the issue.
    RgDs,
    M KVG

    In addition to the Error Message you are getting, it would be helpful to know which version of Oracle Forms you are using and how your application is deployed (Client/Server or Web). If you app is web deployed, then also include the Java Runtime Environment version and your web browser and version.
    Craig B-)
    If someone's response is helpful or correct, please mark it accordingly.

  • Commit of master-detail view with database trigger on update

    [ADF Jdev11 question]
    My case is the following:
    I have master form, with detail table attatched to it (using view link).
    On update of master table, there is a database trigger which makes changes in the detail table.
    As commit action commits both of the tables, as a result, I get the following exception:
    "Another user has changed the row with primary key oracle.jbo.Key[000000002017 ]. "
    Do you have any suggestions how can I fix this? Is there a way I can refresh the detail table just after the commit of the master table?
    Marking the updated attributes with "refresh after update" doesn't help at all.

    This trigger is on update of the master table and that is what it does:
    BEGIN
    UPDATE detail_table
    SET smolotpr = :NEW.molotpr,
    smolpol = :NEW.molpol,
    spoluch = :NEW.poluch,
    sotpr = :NEW.otpr,
    SMAKET = :NEW.MAKET,
    sdata_period = :NEW.data_period
    WHERE doknom = :NEW.doknom;
    END;
    Setting the updated attributes "Refresh after update+insert" to true doesn't help at all (maybe because they are in another table)!
    I will be very grateful if you give me any ideas, advice or give the complete solution!

  • Workflow Vs Database Trigger

    Hi
    I am new to workflow..i am in the learning process.. just a simple query on workflow...
    What is the advantage of Workflow over the database trigger in the context of sending a mail...
    i will be helpful if u send me some features of workflow...
    Regards
    Yram

    Hi,
    If all you want is to send an email, then there are better ways of doing it than to use Workflow.
    Comparing Oracle Workflow to a database trigger is kind of like comparing a car and a unicycle - they do completely different things, but can do the same thing as each other if you really want to.
    Oracle Workflow provides a complete workflow process management solution - a database trigger is a piece of PL/SQL which fires on an insert, update or delete operation on a table. They are completely different.
    It might be worth looking in somewhere like Wikipedia to see what a workflow product can do, or the Workflow pages on OTN to see what Oracle Workflow in particular can offer.
    HTH,
    Matt
    WorkflowFAQ.com - the ONLY independent resource for Oracle Workflow development
    Alpha review chapters from my book "Developing With Oracle Workflow" are available via my website http://www.workflowfaq.com
    Have you read the blog at http://thoughts.workflowfaq.com ?
    WorkflowFAQ support forum: http://forum.workflowfaq.com

  • Invoke Report From Database trigger

    Hi,
    Is it possible to invoke a report from a database trigger?
    The requirement is to start a report after a record was inserted in a table.
    Thank you,
    Alex.

    Hi
    I was looking for exactly the same thing. I need to invoke a report in response to an update in the database. To make things even more complicated, I want to send the report to an email add. Is that something anyone has already taken a look at? Any help appreciated.
    _*                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Logon Database Trigger

    I want to write a database trigger that fires when a user logs onto the database. Oracle has this in 8i+.
    I am using 9i.
    I want to use this trigger to determine if the session is questionable(ie. Hacker). If it is then i want to either kill the session or audit it.
    Auditing is turning on for the database.
    I am trying to use
    execute immediate 'audit all by %username%'
    where username is from the v$session for the current session.
    It appears the logon trigger is fired but the session is not being audited.
    My question is how can I make the session be audited from within a trigger?
    Thanks for you help.
    Daniel

    ro**** wrote:
    When I login in sqlplus I get the error from the after login trigger, but after I get logged in I can issue ALTER SESSION ENABLE RESUMABLE and it works fine (session altered) So why does it let me alter the session after I get logged in, but not during the after logon trigger.It's probably because you have the privilege granted to a role and not a user. Roles are disabled when it comes to definer's rights (default).

  • Database trigger cannot read the updating table

    I want to create a database trigger :
    after insert or update on EMP
    for each row
    begin
    select sum(EMP.salary) from EMP WHERE .....
    end;
    However, I got an error is that I cannot read the updating table if the trigger is 'for each row'. Can anyone help me to to this?
    More, I must use 'for each row' trigger for this case.
    null

    I think the SQL statement should be
    strSQL = "SELECT * FROM MaterialLotJobJoint WHERE JobID=" & _
    tempvars!JobID & " AND MatLotID=" & tempvars!MatLotID
    This assumes thatJobID and MatLotID are number fields.
    Regards, Hans Vogelaar (http://www.eileenslounge.com)

  • Debug Stored procedure or database trigger by 10g Forms

    Hi,
    I want to debug my table level trigger (On update for each row), I am updating this table by forms. When I debug this form I am unable to watch the database debug.
    Please help me regarding database trigger/function/procedures by debugging forms.

    not possible via forms
    you can debug it on SQL Developer by using manual INSERT Updates commands

Maybe you are looking for

  • Print/Email PDF in iBooks

    So, here is what the support page for iOS 4.2 & iBooks says I can do: Printing or Emailing a PDF You can use iBooks to send a copy of a PDF via email, or to print all or a portion of the PDF to a supported printer. Email a PDF: Open the PDF, then tap

  • Upgrade from Windows Server 2012 Active Directory to Windows Server 2012 R2 Active Directory

    We are currently running Windows Server 2012 Active Directory and would like to upgrade to Windows Server 2012 R2 AD. Is it OK to just do an in-place upgrade, or is it advisable to build new domain controllers on R2? Are there any guides or articles

  • Other itunes on network not showing up in source

    hello we have a network of several macs. on each mac we have share i tunes set up but other peoples itunes not showing up in source. can anyone help? firewall port is checked for itunes, and itunes prefs say share my itunes. apple sharing is on. each

  • Unable to create node /home/ SID adm with type directory

    I am currently experiencing a problem when trying to install a Java Stack onto my new SCM 7.0  EHP1 server. I receive the error: System call failed.  Error 89......mkdir..../home/djdadm.... (djd is the SID of my new Java AS) This is followed by an er

  • Lost photos from SD cards in Mac while importing new pictures from iPhone 5

    I imported photos to my MBP iPhoto from my camera, I subsequently selected those photos from my SD cards. After that I tried to import photos from my iPhone 5S to the same MBP. At the end there was an error message saying importing could not be done.