【问题标题】:Get table does not exist error, when trying to insert into table from a trigger尝试从触发器插入表时,获取表不存在错误
【发布时间】:2020-03-17 08:20:00
【问题描述】:

我正在尝试使用触发器来填充另一个表的值。触发器监视表评级的插入并更新另一个表top5restaurants 的值。我还没有弄清楚如何只维护top5restaurants 中的top 5,我不知道如何将表限制为一定数量的条目。但现在我似乎无法从触发器内对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 错误。

【问题讨论】:

    标签: sql oracle oracle11g oracle-sqldeveloper


    【解决方案1】:

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

    你必须去掉括号:

    替换

    insert into top5restaurants values(top5);
    

    insert into top5restaurants values top5;
    

    干杯!!

    【讨论】:

      【解决方案2】:

      您没有发布所有涉及的表格,但是 - 很明显,view 被创建为

      create view top_rest ... from rating
                                    ------
      

      触发器 被创建为

      after insert on ratings
                      -------
                            ^
                            s?
      

      是哪一个? ratingratings?

      【讨论】:

      • 它的评分,对不起。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 2016-06-17
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多