我正在尝试创建一个简单的触发器,但我得到了以下错误。我在互联网上搜索但找不到解决方案。你能帮我解决这个问题吗?
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”。
答案 0 :(得分:1)
如果这是你的想法"一个简单的触发器"那我想知道复杂的想要什么?
SP2-0552
错误似乎可能是因为您在没有设置SQLBLANKLINES的情况下运行了带有流氓换行符的脚本。
但是一旦你修复了语法错误,你就会发现你的触发器由于变异表错误而无法运行。我们无法从基础表中选择触发器,因为状态是不确定的。所以这是错误的:
select max(t039c003) into maxconsecutivo
from T039
where t071c002=:new.t039c004;
您需要找到一种不同的方式来实现应该执行的任何业务规则。
答案 1 :(得分: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;
我怀疑你得到的错误是由于对列的无效引用(当使用:new时)
您可以尝试以下触发器和功能:
autonomous_transaction
函数以插入最初的" consecutivo" 在触发器中,以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;
/