首页 > 解决方案 > 尝试从触发器插入表时,获取表不存在错误

问题描述

我正在尝试使用触发器来填充另一个表的值。触发器监视表评级上的插入并更新另一个表的值top5restaurants。我还没有弄清楚如何只维护top 5in top5restaurants,我不知道如何将表限制为一定数量的条目。但现在我似乎无法top5restaurants在触发器内做任何事情。

drop view top_rest;

create view top_rest (rid, rat) 
as
    (select distinct rid, max(stars) 
     from rating
     group by rid);

drop table top5restaurants;

create table top5restaurants(rid int);

insert into top5restaurants(rid)
    select rid from top_rest
    where rownum <= 5
    order by rat asc;

create or replace trigger top5_trigger
after insert on ratings
for each row
    declare top5 top5restaurants%rowtype;

    cursor top5_cursor is
    select rid from top_rest
    where rownum <=5
    order by rat;
    begin
    for record in top5_cursor
    loop

        fetch top5_cursor into top5; 
        insert into top5restaurants values(top5);
    end loop;
    end;
    /
--    
--
begin
update_reviews('Jade Court','Sarah M.', 4, '08/17/2017');
update_reviews('Shanghai Terrace','Cameron J.', 5, '08/17/2017');
update_reviews('Rangoli','Vivek T.',3,'09/17/2017');
update_reviews('Shanghai Inn','Audrey M.',2,'07/08/2017');
update_reviews('Cumin','Cameron J.', 2, '09/17/2017');
end;
/    
select * from top5restaurants;
insert into top5restaurants values(184);

但是,该表确实存在,我可以对其运行查询,它会返回我在创建表时插入的数据。我也可以插入值。不知道为什么我在使用触发器时得到 table not found 错误。

标签: sqloracleoracle11goracle-sqldeveloper

解决方案


trigger除了and中的表名不同(Littlefoot 回答) ,您在插入数据时view没有正确使用集合。rowtype

您必须删除括号:

代替

insert into top5restaurants values(top5);

insert into top5restaurants values top5;

干杯!!


推荐阅读