Create trigger to insert into another table

前端 未结 4 1536
一向
一向 2021-01-26 08:09

I have some problem executing the trigger below:

CREATE OR REPLACE TRIGGER AFTERINSERTCREATEBILL
AFTER INSERT
ON READING
FOR EACH ROW 

DECLARE

varReadNo   Int;         


        
相关标签:
4条回答
  • 2021-01-26 08:49

    Add exception handling in your trigger and see what is happening, by doing it would be easy for you to track the exceptions.

    CREATE OR REPLACE TRIGGER AFTERINSERTCREATEBILL
    AFTER INSERT
    ON READING
    FOR EACH ROW 
    
    DECLARE
    
      varCustID   Varchar(10);
    
    BEGIN 
    
      -- your code
    
    EXCEPTION
        WHEN NO_DATA_FOUND
        THEN
            DBMS_OUTPUT.PUT_LINE(TO_CHAR(SQLERRM(-20299)));
        WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE(TO_CHAR(SQLERRM(-20298)));
    
    END;
    
    0 讨论(0)
  • 2021-01-26 08:51

    Make sure that you have the necessary permissions on all the tables and access to the sequences you're using in the insert.

    I haven't done Oracle in awhile, but you can also try querying dba_errors (or all_errors) in order to try and get more information on why your SP isn't compiling.

    Something to the tune of:

    SELECT * FROM dba_errors WHERE owner = 'THEOWNER_OF_YOUR_SP';
    
    0 讨论(0)
  • 2021-01-26 08:59

    You cannot retrieve records from the same table in a row trigger. You can access values from actual record using :new and :old (is this your case?). The trigger could then be rewritten to

    CREATE OR REPLACE TRIGGER AFTERINSERTCREATEBILL
    AFTER INSERT
    ON READING
    FOR EACH ROW 
    
    DECLARE
    
      varCustID   Varchar(10);
    
    BEGIN 
    
      Select CustID INTO varCustID
        From Address A
        Join Meter M 
          on A.postCode = M.postCode
        Where M.MeterID = :new.MeterID;
    
      INSERT INTO BILL VALUES 
      (SEQBILLNO.NEXTVAL, SYSDATE, 'UNPAID' , 100 , varCustID , SEQREADNO.CURRVAL); 
    
    END;
    

    If you need to query other record from READING table you have to use a combination of statement triggers, row trigger and a PLSQL collection. Good example of this is on AskTom.oracle.com

    0 讨论(0)
  • 2021-01-26 09:05

    we can combined insert and select statement

    CREATE OR REPLACE TRIGGER AFTERINSERTCREATEBILL
    AFTER INSERT
    ON READING
    FOR EACH ROW 
    
    DECLARE
    
    varCustID   Varchar(10);
    
    BEGIN 
    
     insert into bill 
     values 
     select SEQBILLNO.NEXTVAL, 
            SYSDATE, 
            'UNPAID' , 
            100 ,
            CustID,SEQREADNO.CURRVAL 
     From  Address A
     Join  Meter M 
     on    A.postCode = M.postCode
     Where M.MeterID = :new.MeterID; 
    
    END;
    

    try the above code.

    0 讨论(0)
提交回复
热议问题