2016-08-03 39 views
0

我試過兩種方法。首先,我使用存儲過程在觸發器中調用。錯誤刷新或更新使用觸發器的物化視圖

CREATE OR REPLACE TRIGGER MV_BANK_CODE_UPDATE 
AFTER UPDATE OR INSERT OR DELETE OF APPROVAL_STATUS ON BANK_CODES 
FOR EACH ROW 
    BEGIN 


    SP_UPDATE_MV_BANK_CODES; 


    END; 
/

存儲過程:

CREATE OR REPLACE PROCEDURE SP_UPDATE_MV_BANK_CODES IS 


BEGIN 

DELETE MV_BANK_CODES; 
COMMIT; 
INSERT MV_BANK_CODES 
(SELECT DISTINCT 
     bankcodeeo.clearing_area, 
     bankcodeeo.bank_code, 
     bankcodeeo.bank_code_id, 
     bankcodeeo.bank_name, 
     bankcodeeo.office_code, 
     bankcodeeo.bank_address, 
      oldaccounteo.office_code 
     || '.' 
     || oldaccounteo.gl_account_no 
     || '.' 
     || oldaccounteo.sl_account_no 
     || '.' 
     || oldaccounteo.currency_code 
     || '.' 
     || oldaccounteo.department_code 
     || '.' 
     || oldaccounteo.working_unit_code 
     || '.' 
     || oldaccounteo.project_code 
      AS new_ind_account_no, 
     /*tcgleo.*/ 
     '9' AS position, 
     transactioncodeeo.transaction_code, 
     oldaccounteo.gl_account_no, 
     oldaccounteo.sl_account_no, 
     oldaccounteo.currency_code, 
     oldaccounteo.department_code, 
     oldaccounteo.working_unit_code, 
     oldaccounteo.project_code, 
     oldaccounteo.old_ind_account_no, 
     oldaccounteo.old_ind_account_name, 
     transactioncodeeo.forex_type_flag, 
     transactioncodeeo.forex_type, 
     glaccounteo.gl_account_type AS classification_flag, 
     bankcodeeo.member_code, 
     oldaccounteo.category_code, 
     glaccounteo.normal_position, 
     transactioncodeeo.source_account_flag, 
     transactioncodeeo.usage_forex, 
     bankcodeeo.member_type, 
     TRANSACTIONCODEEO.CURRENCY_TYPE 
    FROM bank_codes bankcodeeo 
     INNER JOIN old_accounts oldaccounteo 
      ON  bankcodeeo.account_no = oldaccounteo.old_ind_account_no 
      AND oldaccounteo.approval_status = '2' 
      AND oldaccounteo.activation_status = '1' 
     INNER JOIN gl_sl_accounts glsleo 
      ON  glsleo.gl_account_no = oldaccounteo.gl_account_no 
      AND glsleo.sl_account_no = oldaccounteo.sl_account_no 
      AND glsleo.approval_status = '2' 
      AND glsleo.activation_status = '1' 
     INNER JOIN gl_accounts glaccounteo 
      ON  glaccounteo.gl_account_no = glsleo.gl_account_no 
      AND glaccounteo.approval_status = '2' 
      AND glaccounteo.activation_status = '1' 
     INNER JOIN tc_gl tcgleo 
      ON tcgleo.gl_account_no = glaccounteo.gl_account_no 
     INNER JOIN transaction_codes transactioncodeeo 
      ON  transactioncodeeo.transaction_code = tcgleo.transaction_code 
      AND transactioncodeeo.approval_status = '2' 
      AND transactioncodeeo.activation_status = '1' 
WHERE bankcodeeo.approval_status = '2' 
     AND bankcodeeo.activation_status = '1'); 

     COMMIT; 

END; 
/

當我更新表BANK_CODE,我得到了一個錯誤:

ORA-01732: data manipulation operation not legal on this view 
ORA-06512: at "SP_UPDATE_MV_BANK_CODES", line 6 
ORA-06512: at "MV_BANK_CODE_UPDATE", line 3 
ORA-04088: error during execution of trigger 'MV_BANK_CODE_UPDATE' 

其次,我用DBMS_MVIEW.REFRESH

CREATE OR REPLACE TRIGGER MV_BANK_CODE_UPDATE_X 
AFTER UPDATE OR INSERT OR DELETE OF APPROVAL_STATUS ON BANK_CODES 

FOR EACH ROW 
    BEGIN 

    DBMS_MVIEW.REFRESH('MV_BANK_CODES'); 

    END MV_BANK_CODE_UPDATE_X; 
/

當我updat編輯表BANK_CODE,我得到一個錯誤:

ORA-04092: cannot COMMIT in a trigger 
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 3014 
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 2994 
ORA-06512: at "MV_BANK_CODE_UPDATE_X", line 3 
ORA-04088: error during execution of trigger 'MV_BANK_CODE_UPDATE_X' 

我該怎麼辦?

回答

0

可能是我理解你的要求,你需要刷新物化視圖MV_BANK_CODES,每當任何dml操作發生在表BANK_CODES上。

幾點必須注意

  1. 的首先無需手動編寫代碼使用觸發器來刷新MV。
  2. 我們不能在觸發器中進行提交/回滾。
  3. DBMS_MVIEW.REFRESH隱含承諾,因此它不可能觸發內提交

BAD解決方案: 您可以通過使用觸發器,它把插入/更新內部PRAGMA自主塊做/刪除裏面的MV作爲單獨的事務,因此mv將具有未提交的數據,並且如果事後失敗,mv數據將不會被回滾。

Preferrable solutons: 現在如果你想刷新物化視圖,你需要了解你如何定義你的物化視圖Documentation Materialized View Refresh

create materialized view MV_BANK_CODES 
    REFRESH FAST ON COMMIT 
AS 
    SELECT DISTINCT 
     bankcodeeo.clearing_area, 
     bankcodeeo.bank_code, 
     bankcodeeo.bank_code_id, 
     bankcodeeo.bank_name, 
+0

我找到了一個鏈接,其中賈斯汀洞穴精美回答的http:// stackoverflow.com/questions/13068366/how-to-refresh-materialized-view-using-trigger –