[英]Create trigger using cursor
I am having difficulty figuring out how to implement a before insert
trigger using a cursor that will compare the insert number with the max value
of the 1 column table (NUM)
, then print out the corresponding message. 我很难弄清楚如何使用游标实现
before insert
触发器,该游标将把插入数与1列表(NUM)
的max value
进行比较,然后打印出相应的消息。 Any help would be greatly appreciated. 任何帮助将不胜感激。
create or replace trigger MAXSOME
before insert on SOMENUMBERS for each row
declare
cursor pointer is
select max(NUM) from SOMENUMBERS;
x number;
begin
x := :new.NUM;
if x > pointer.num then
dbms_output.put_line('The new number ' || x || ' is greater than the greatest number in the table.');
elsif x := pointer then
dbms_output.put_line('The new number ' || x || ' is the same as the greatest number in the table.');
else
dbms_output.put_line(pointer.num || ' is still the largest number in the table.');
end if;
end;
/
If you declare a cursor you have to open it in a loop but you don't even need a cursor. 如果声明一个游标,则必须循环打开它,但是甚至不需要游标。 Using dbms_output is not going to work very well as one session may insert and you may be waiting for the output using another session.. Create another table and log the output there.
使用dbms_output不能很好地工作,因为可能插入一个会话,并且您可能正在等待使用另一会话的输出。.创建另一个表并在此处记录输出。
I see Knuckles suggests an autonomous transaction but my test case succeeded without one. 我看到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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.