Sql 使用游标创建触发器
我很难弄清楚如何在insert触发器之前使用光标执行Sql 使用游标创建触发器,sql,oracle,plsql,triggers,cursor,Sql,Oracle,Plsql,Triggers,Cursor,我很难弄清楚如何在insert触发器之前使用光标执行,该光标将insert编号与1列表格(NUM)的最大值进行比较,然后打印出相应的消息。任何帮助都将不胜感激 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
,该光标将insert编号与1列表格(NUM)
的最大值进行比较,然后打印出相应的消息。任何帮助都将不胜感激
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;
/
如果你声明了一个游标,你必须在一个循环中打开它,但你甚至不需要一个游标。使用dbms_输出不会很好地工作,因为一个会话可能会插入,而您可能正在使用另一个会话等待输出。。创建另一个表并在其中记录输出。
我看到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;
我很想知道这样一个请求的用例。在任何情况下,如果不使用pragma_autonomy_事务,您都无法对定义触发器的表执行此操作。感谢您的回复。这是针对学校布置的作业,因此用例将用于知识的应用?在这个特定示例中,它要求使用显式游标。