列插入时触发错误

时间:2014-11-21 20:18:16

标签: sql oracle

我正在为insert创建一个触发器。如果其中一个值与旧表中的值相同。然后我打印一条消息。这是我的代码。

Create or replace  trigger TR_insert_act
After INSERT On ACTIVITIES
For each row 

DECLARE
l_act varchar(30);

Begin 

select Activity into l_act
From ACTIVITIES;

if(:new.Activity in l_act) then
DBMS_OUTPUT.PUT_LINE ('There is duplicate.'); 
end if;

end; 

在选择l_act时没有编译错误,该怎么办?

1 个答案:

答案 0 :(得分:0)

您需要BEFORE触发器和WHERE条件。

DBMS_OUTPUT不会显示错误。

Create or replace  trigger TR_insert_act
before INSERT On ACTIVITIES
For each row 

l_act number;
Begin 

select count(1) into l_act
From ACTIVITIES
WHERE ACTIVITY = :new.Activity

if(l_act > 0 ) then
  RAISE_APPLICATION_ERROR (
     num => -20000,
     msg => 'There is Duplicate');
end if;

end;