簡體   English   中英

oracle 頂點 sql 的簡單觸發/程序問題

[英]simple trigger/procedure problem for oracle apex sql

我試圖在 oracle 頂點 sql 中運行此審計跟蹤觸發器,但我一直收到同樣的錯誤,我不知道我做錯了什么。 我還需要對數據庫中的每個表執行相同的觸發器...有沒有辦法我可以通過一個過程執行相同的操作以便只執行一次?


create or replace TRIGGER AUDIT_TRAIL_USERS_TRIG
  -- starts on every update, insert or delete command
  AFTER INSERT OR DELETE OR UPDATE ON USERS
  FOR EACH ROW
DECLARE
  -- variable which declares if update, delete or insert process
  v_trg_action varchar2(10);
BEGIN
  IF updating  THEN
    -- when update
    v_trg_action := 'UPDATE';
  ELSIF deleting  THEN
    -- when delete
    v_trg_action := 'DELETE';
  ELSIF inserting  THEN
    -- when insert
    v_trg_action := 'INSERT';
  ELSE
    -- if something else
  END IF;
  IF v_trg_action IN ('DELETE','UPDATE','INSERT') THEN
      -- if v_trg_action is DELETE, UPDATE OR INSERT then insert old table values
   INSERT INTO AUDIT_TRAIL
  ( AUDIT_USER,  AUDIT_DATE,  AUDIT_ACTION)
   VALUES
  (UPPER(v('APP_USER')), SYSDATE, v_trg_action);
  ELSE
  END IF;
  -- about the insert command on the audit table
  -- for current apex user: v('APP_USER')
  -- for date: SYSDATE
  -- for sql command: v_trg_action   
END AUDIT_TRAIL_USERS_TRIG;

我得到的錯誤(我確定我比它對我說的更多)如下:

編譯失敗,第 16 行 (03:29:53) 與編譯錯誤相關的行號與第一個 BEGIN 語句相關。 這只會影響數據庫觸發器的編譯。

PLS-00103: Encountered the symbol "END" when expecting one of the following: ( begin case declare exit for goto if loop mod null pragma raise return select update while with << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge json_exists json_value json_query json_object json_array Compilation failed, line 25 (03:29:53) 與編譯錯誤相關的行號是相對於第一個 BEGIN 語句的,這只影響數據庫觸發器的編譯。

PLS-00103: Encountered the symbol "END" when expecting one of the following: ( begin case declare exit for goto if loop mod null pragma raise return select update while with << continue close current delete fetch lock insert open rollback savepoint set sql execute提交 forall 合並 pipe 清除 json_exists json_value json_query json_object json_array

IF..ELSE 塊不能為空。 如果您不需要它們將其刪除,我添加了一個虛擬NULL調用以編譯代碼。根據需要添加適當的邏輯,否則刪除該塊。

create or replace TRIGGER AUDIT_TRAIL_USERS_TRIG
  -- starts on every update, insert or delete command
  AFTER INSERT OR DELETE OR UPDATE ON USERS
  FOR EACH ROW
DECLARE
  -- variable which declares if update, delete or insert process
  v_trg_action varchar2(10);
BEGIN
  IF updating  THEN
    -- when update
    v_trg_action := 'UPDATE';
  ELSIF deleting  THEN
    -- when delete
    v_trg_action := 'DELETE';
  ELSIF inserting  THEN
    -- when insert
    v_trg_action := 'INSERT';
  ELSE
    -- if something else
    NULL;
  END IF;
  IF v_trg_action IN ('DELETE','UPDATE','INSERT') THEN
      -- if v_trg_action is DELETE, UPDATE OR INSERT then insert old table values
   INSERT INTO AUDIT_TRAIL
  ( AUDIT_USER,  AUDIT_DATE,  AUDIT_ACTION)
   VALUES
  (UPPER(v('APP_USER')), SYSDATE, v_trg_action);

  null;
  ELSE
   NULL;
  END IF;
  -- about the insert command on the audit table
  -- for current apex user: v('APP_USER')
  -- for date: SYSDATE
  -- for sql command: v_trg_action   
END AUDIT_TRAIL_USERS_TRIG;

使用此代碼,它將像您的一樣工作

create or replace TRIGGER AUDIT_TRAIL_USERS_TRIG
  AFTER INSERT OR DELETE OR UPDATE ON USERS
  FOR EACH ROW
DECLARE
BEGIN
    IF inserting or updating or deleting  THEN
        INSERT INTO AUDIT_TRAIL
            (AUDIT_USER,  AUDIT_DATE,  AUDIT_ACTION)
        VALUES
            (UPPER(v('APP_USER')), SYSDATE, v_trg_action);
    END IF;  
END AUDIT_TRAIL_USERS_TRIG;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM