Mor*_*hai 0 sql oracle triggers
我有一个触发器来验证字段是否为空:
create or replace trigger trig1
after insert on table_1
for each row
begin
if ((select table2.column2 from table2 where table2.id= :new.id) isnull) then
update table2 set table2.column2 = :new.column1 where table2.id = :new.id;
end if;
end trig1;
.
run;
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,指出没有正确创建触发器.我不知道问题是什么.我使用的是Oracle SQL*Plus 10.2.0
PL/SQL语法不允许在IF子句中包含SQL语句.
正确的方法是分离出SELECT语句,然后测试其结果.那就是:
create or replace trigger trig1
after insert on table_1
for each row
declare
v table2.column2%type;
begin
select table2.column2
into v
from table2
where table2.id= :new.id;
if v is null
then
update table2
set table2.column2 = :new.column1
where table2.id = :new.id;
end if;
end trig1;
Run Code Online (Sandbox Code Playgroud)
请注意,这不会在table2匹配条件时处理多行的存在,或者实际上没有匹配的行.它也不处理锁定.
另外,请记住,像这样的代码在多用户环境中不能很好地运行.这就是我提到锁定的原因.您应该使用过程逻辑来处理这些类型的需求.尽管通常是错误构想触发器的情况,但真正的罪魁祸首是数据模型不佳. table2.column2应该已经正常化了.