0

この質問が何度も考え出されたことは知っていますが、あなたのアドバイスが必要です:)

2つのテーブルを持つ:

  • 販売収入

| item |income |create_user |create_date |last_update_user | update_time| |------|-------|------------|-------------------|-----------------|------------| | 1 | 100 |duck |05-19-2016 |human |05-19-2016 | | 2 | 250 |dog |05-19-2016 |human |05-19-2016 | | 3 | 210 |cat |05-20-2016 |human |05-19-2016 |

  • Sale_income_audit

| item |change_id|last_inc|new_inc |user_update|update_date|operation | |------|---------|--------|----------|-----------|-----------|------------| | 1 | 1 |null |05-19-2016|duck |05-19-2016 |I | | 2 | 2 |null |05-19-2016|dog |05-19-2016 |I | | 3 | 33 |null |05-20-2016|cat |05-19-2016 |I |

タスクは次のとおりです。誰かが挿入、更新、または削除を行うとき、トリガーは、 Sale_income_auditテーブルにアクションを実行する上記のレコードを挿入する必要があります(操作のタイプ - 列 '操作)。同時に、テーブル Sale_income (last_update_user および update_time) を更新する必要があります。私はこれを行いました:3つの変数でパッケージを作成します:

create or replace package Sale_income_var as
    v_old_income BINARY_INTEGER := null;
    v_new_income BINARY_INTEGER := null;
     v_item      BINARY_INTEGER := null;
END Sale_income_var;

および2つのトリガー

最初

create or replace trigger audit_income_IUD
  after insert or update or delete on Sale_income
    for each row
begin  
    .
    .                
elsif updating then  
        Sale_income_var.v_old_income  := :old.income;
        Sale_income_var.v_new_income  := :new.income;                     
             if  Sale_income_var.v_item is null then                      
        Sale_income_var.v_item := :old.item;
        DBMS_OUTPUT.PUT_LINE(Sale_income_var.v_item);
            end if;
     .
     .
end if;
end audit_income_IUD;

2番目

create or replace trigger  sale_income_au
  after update of income on Sale_income
 begin 
   update Sale_income set last_update_user = user, last_update_date = sysdate
       where item = Sale_income_var.v_item;     
   INSERT into Sale_income_audit   (item, 
                                  change_id, 
                                  last_income, 
                                  new_income, 
                                  user_update, 
                                  update_date, 
                                  operation)
                             VALUES (Sale_income_var.v_item,
                                     auto_incr.NEXTVAL, 
                                     Sale_income_var.v_old_income, 
                                     Sale_income_var.v_new_income,
                                     user,
                                     sysdate,
                                     'U');                         
             Sale_income_var.v_item := null;
    end sale_income_au;

それはうまくいきますが、それが間違った解決策であると私は落ちました。「一般的な」トリガーからの更新ブロックが別のトリガーに移動し、変数を使用したこの魔法はうまくいかないので、そうですか?

このタスクをどのように決定し、私のソリューションで何を変更しますか? 手伝ってくれてありがとう :)

4

2 に答える 2

0

答えは単純に見えました。各行の後に使用し、ブロック「更新」に追加する必要があります

    :new.last_update_user := user;
    :new.last_update_date := sysdate;

パッケージなしでトリガーを1つに制限できます!

于 2016-05-21T21:27:20.927 に答える