使用游标创建触发器

时间:2015-10-28 17:46:57

标签: sql oracle plsql triggers cursor

我很难弄清楚如何使用游标来实现before insert触发器,该游标将插入数字与1列表max value的{​​{1}}进行比较,然后打印出来相应的消息。任何帮助将不胜感激。

(NUM)

1 个答案:

答案 0 :(得分:0)

如果你声明一个光标,你必须在一个循环中打开它,但你甚至不需要一个光标。使用dbms_output不会很好地工作,因为一个会话可能会插入,您可能正在等待另一个会话的输出。创建另一个表并将输出记录在那里。
我看到Knuckles建议进行自主交易,但我的测试用例没有成功。

create or replace trigger MAXSOME
before insert on SOMENUMBERS
for each row
declare

    x SOMENUMBERS.NUM%TYPE;
begin
    select max(NUM) into x from SOMENUMBERS;


    if x > :new.NUM then
        dbms_output.put_line('The new number ' || :new.NUM || ' is greater than the greatest number in the table.');
    elsif x = :new.NUM then
        dbms_output.put_line('The new number ' || :new.NUM || ' is the same as the greatest number in the table.');
    else
       dbms_output.put_line(:new.NUM || ' is still the largest number in the table.'); 
    end if;
end;
相关问题