我有兩個表。表A像創建一個處理兩個表的觸發器
Name color
a1 red
a2 yellow
a3 black
a4 blue
表B就像
Name minutes action
b1 10 jump
b2 20 run
b3 40 dance
我創建一個觸發器表B中插入行,如果動作是B.存在然後我打印新添加的信息。另外,如果新加入的人的顏色是紅色,我會算表B中有更多/更少分鐘多少人的代碼是一樣
create or replace trigger TR_insert_count
Before INSERT On B
For each row
DECLARE
l_act integer;
l_less integer;
l_more integer;
l_equal integer;
Begin
select count(1) into l_act
From B
Where Action=:new.Action;
select count(*)
into l_less
From B
Where Action=:new.Action and MINUTES > :new.minutes;
select count(*)
into l_more
From B
Where Action=:new.Action and MINUTES < :new.minutes;
select count(*)
into l_equal
From B
Where Action=:new.Action and MINUTES = :new.minutes;
if(l_act>0) then
DBMS_OUTPUT.PUT_LINE ('There is duplicate.');
DBMS_OUTPUT.PUT_LINE ('The new input info is with name ' || :new.Name || ' with
activity ' || :new.action ||' for ' ||:new.minutes || ' minutes.');
if(:new.name in (select Name From A where color='red')) then
DBMS_OUTPUT.PUT_LINE('There are '||l_more ||'people having more minutes.');
DBMS_OUTPUT.PUT_LINE('There are '||l_less ||'people having less minutes.');
DBMS_OUTPUT.PUT_LINE('There are '||l_equal ||'people having the same minutes.');
end if;
end if;
end
它被編譯,但插排時,報告錯誤說觸發無效。我想知道是因爲我在這裏有行級別或表級觸發器,請問該怎麼辦?
謝謝,修正了,但還是想着如何解決它,也許把它分成兩個觸發器。 – 2014-11-24 15:44:40
2觸發器不會有幫助 - 您需要按照文章中的描述使用3。另一種選擇是創建存儲過程,將數據插入'B'並打印所需的信息。然後,您可以撤銷除存儲過程之外的任何人的插入權限。注意:你的4個選項可以用一個替換。 – a1ex07 2014-11-24 15:58:05