sql - 尝试从触发器插入表时,获取表不存在错误
问题描述
我正在尝试使用触发器来填充另一个表的值。触发器监视表评级上的插入并更新另一个表的值top5restaurants
。我还没有弄清楚如何只维护top 5
in 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 错误。
解决方案
trigger
除了and中的表名不同(Littlefoot 回答) ,您在插入数据时view
没有正确使用集合。rowtype
您必须删除括号:
代替
insert into top5restaurants values(top5);
和
insert into top5restaurants values top5;
干杯!!
推荐阅读
- javascript - 从表单调用时发出接收元素值
- python - 导入 cartopy 时出错:ModuleNotFoundError: No module named 'cartopy._crs'
- c - 我的第一个 C 代码有问题,我尝试了很多东西,但我总是遇到同样的错误
- php - Laravel CSRF Token 句柄在表单保存,正确使用
- python - 如何检索通向 sklearn 决策树的每个叶节点的完整分支路径?
- java - 弹出创建对话框 - Android studio
- c - 矩阵数据结构和函数参数
- visual-studio - Razor 页面 @events 没有智能感知
- python - 如何在格式化程序中修改 python 日志记录模块名称
- c# - 为什么我无法从我的 Xamarin.Forms UWP 应用程序访问 DownloadsFolder,并获得 System.UnauthorizedAccessException?