2017-10-16 46 views
1

単純なトリガーを作成しようとしていますが、エラーが発生しました。私はインターネットで検索しましたが、解決策を見つけることができませんでした。この問題について私を助けてくれますか?Oracle SP2-0552:バインド変数 "NEW"が宣言されていません

create trigger ProcessTigger before insert on T039 
for each row 
declare consecutivo int; idconsecutivo int; maxconsecutivo int; 
begin 
    select t326c004 into consecutivo from T326 where t326c003 = 'T039' and t326c002 = :new.t039c004; 

if consecutivo is not null 
then 
consecutivo :=consecutivo+1; 
select t326c001 into idconsecutivo from T326 where t326c002 = :new.t039c004 and t326c003=T039; 
update T326 set t326c004 = consecutivo where t326c001=idconsecutivo and t326c003=T039; 
else 
select max(t039c003) into maxconsecutivo from T039 where t071c002=:new.t039c004; 

if maxconsecutivo is not null 
then consecutivo := maxconsecutivo+1; 
else consecutivo:=1; 
end if; 

insert into T326 
(t326c002,t326c003,t326c004)values(:new.t039c004,'T039',consecutivo); 

end if; 
end; 

ERROR:

SP2-0552:バインド変数 "NEW" が宣言されていません。

+1

これはどちらですか? ORA-04071:トリガーにBEFORE/AFTER/INSTEAD OF句がありません。またはSP2-0552、バインド変数 "NEW"は宣言されていませんか? Oracleは一度に1つのエラーをスローします。 – mathguy

+0

申し訳ありません私は質問のタイトルに間違っていました。問題は、またはSP2-0552でした。バインド変数 "NEW"は宣言されていません。 –

+0

あなたの投稿の下に小さな「edit」リンクを使って編集してください。問題に合わせてタイトルを変更できます。 – mathguy

答えて

1

「これは簡単なトリガー」のアイデアなら、複雑なものは何を望んでいるのだろうか?

SQLBLANKLINESを設定せずに不正な改行を含むスクリプトを実行しているため、SP2-0552エラーが発生している可能性があります。

しかし、一度構文エラーを修正すると、テーブルの変更でエラーが発生することがあります。状態は不確定なので、基になるテーブルからトリガで選択することはできません。だからこれは間違っている:

select max(t039c003) into maxconsecutivo 
from T039 
where t071c002=:new.t039c004; 

あなたは、実行すべきビジネスルールを実装する別の方法を見つける必要があります。

+0

私はそれが簡単な引き金だったと言って誇張すると思います!私はすでに設定SQLBLANKLINESを修正しましたが、私はまだ同じエラーが発生します。私は別の方法を見つける必要がありますか? –

0

IDを発行するそのような機能に対してトリガを使用することは安全ではありません。同じIDを得るために競争するインサート以上のものがあるかもしれないことを忘れないでください。

また、行レベルのトリガーで同じテーブルから選択できない変異テーブルの問題。

それに加えて、T039を引用符で囲んでいない以下のような行に構文エラーがあります。

select t326c001 into idconsecutivo from T326 where t326c002 = :new.t039c004 
and t326c003=T039; 
update T326 set t326c004 = consecutivo where t326c001=idconsecutivo and 
t326c003=T039; 

私は(使用している場合:新しい)あなたが得るエラーが列への無効な参照が原因である疑いがある

をあなたは、次のトリガ機能を試すことができます。

  1. 作成autonomous_transaction最初の "continuutivo"を挿入する関数
  2. トリガーでは、insert(関数の呼び出し)で始まり、レコードを作成しない場合は、

    を更新します
    create or replace 
        trigger processtrigger 
        before insert on t039 
        for each row 
        declare 
        v_id number; 
        begin 
        -- start with insert calling the function 
        if f_create_new_con(:new.t039c004) = 0 then 
         update t326 set t326c004 = t326c004 + 1 -- this is safe since it will take the last committed t326c004 and increase it 
         where t326c003 = 'T039' 
         and t326c002 = :new.t039c004; 
        end if; 
        end; 
    /
    
        create or replace 
        function f_create_new_con(p_value in number) return number 
        is 
        pragma autonomous_transaction; 
        begin 
        insert into t326 (t326c002, t326c003, t326c004) 
        select p_value, 'T039', (select nvl(max(t039c003), 0) + 1 from t039 where t071c002 = p_value) 
        from dual 
        where not exists (select 1 from t326 where t326c002 = p_value and t326c003 = 'T039'); 
    
        -- if no insert then return 0 to update 
        if (sql%rowcount = 0) then 
         return 0; 
        else 
         return 1; 
        end if; 
        end; 
    /
    
関連する問題