2017-10-16 64 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; 

错误:

SP2-0552:绑定变量 “NEW” 未声明。

+1

那么,这是什么? ORA-04071,触发器缺少BEFORE/AFTER/INSTEAD OF子句?或者SP2-0552,绑定变量“NEW”没有声明? Oracle一次抛出一个错误,所以它不能**都是。 – mathguy

+0

抱歉,我在问题的标题中错了。问题是或SP2-0552,绑定变量“新”没有声明 –

+0

使用您的文章下方的小'编辑'链接进行编辑 - 您可以更改标题以符合您的问题。 – mathguy

回答

1

如果这是你的“简单的触发器”的想法,那么我想知道一个复杂的想要什么?

看起来SP2-0552错误可能是因为您正在运行带有流氓换行符的脚本而没有设置SQLBLANKLINES

但是,一旦你已经修复了语法错误,你会发现你的触发器将不会运行,由于表中的变异错误。我们无法从底层表中选择触发器,因为状态是不确定的。所以这是错误的:

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

你需要找到一种不同的方式来实现任何应该做的业务规则。

+0

我想我夸大说这是简单的触发器!我已经更正了设置SQLBLANKLINES,但我仍然得到相同的错误。我需要找到一种不同的方式? –

0

对于这种分配ID功能的触发器的使用是不安全的。请记住,可能会有不止一个插入比赛来获得下一个'consecutivo',并获得相同的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功能插入初始“consecutivo”
  2. 在触发器中,从插入(调用函数)开始,如果不创建记录,则更新

    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; 
    /