请检查我的代码,我很少用触发器与AFTER子句,非常感谢:触发器在Oracle中:没有找到数据
set serveroutput on;
create or replace trigger tvideo_2 after insert on video for each row
declare
pragma autonomous_transaction;
v_title video.title%type; /* Declare a variable to check title that contain '18+' */
begin
select title into v_title from video where video.videoid=:new.videoid;
if v_title like '%18+%' then
update video
set age=18
where videoid=:new.videoid;
dbms_output.put_line('Video '||:new.videoid||' has been updated age to 18+');
else
dbms_output.put_line('Video '||:new.videoid||' is not 18+!');
end if;
end;
/
insert into video values('V5', '18+', 240, 19);
VIDEO properties: (videoid, title, duration, age)
这不是触发器的最佳用法。您正在使用自主事务来解决突变触发器错误,但这增加了其他问题的可能性。当从触发器调用DBMS_output时不会显示任何内容。为什么不使用插入前设置年龄? – kevinsky
我猜你想要使用插入或更新触发器之前,你想要使用':new.title'而不是从'video'中选择,并且你想要修改':new.title'而不是在'video'上做'更新'。然而,当你有错误时,包含错误而不是希望我们能猜出错误是什么(并且确切地说你想要什么行为)会更有意义。用合理的缩进格式化你的代码也是很好的。 –
如果我创建一个包含'V5'的数据,它会工作正常(我必须删除videoido是主键,没有任何限制)。我经常在before子句中使用trigger来检查插入的行并调用raise_app_error。但是在这种情况下,您必须插入一行,然后触发器会检查标题并更新标题是否包含18+,否则不打印“不是18+” - 这意味着触发后(Oracle课程的要求:练习考试。已经完成了它,它不能运行...)。对不起,我的英语不好。 xD – mmo2112