首页 > 解决方案 > 它不工作。任何人都可以帮我解决它吗?

问题描述

我创建了这个程序,但它说“PLS-00364:循环索引变量'DS'使用无效”和“ORA-00942:表或视图不存在”。我该如何解决?

 create or replace procedure thangmax3(
        nam IN number)
    as
    begin
        nam:=&nam;
        dbms_output.put_line('DS 3 thang nhieu khach den o ksan nhat nam '||nam);
        for ds in (select d.thang, d.sokhach from (select extract(month from thoigiannhan)as thang,COUNT(mathuephong)sokhach from HR.thong_tin_thue_phong 
                                    where extract(year from thoigiantra)=nam 
                                    group by  extract(month from thoigiannhan)
                                    order by COUNT(mathuephong) DESC) d 
                        where rownum<=3)
        loop
            dbms_output.put_line('Thang: '||ds.thang||'     '||'So khach: '||ds.soluong);
        end loop;
    end;

标签: oracleoracle11g

解决方案


那么只有代码中涉及的“真实”表是

hr.thong_tin_thue_phong

由于您在其名称前加上所有者名称 ( hr),我猜您当前未连接为hr. 如果是这样,hr应授予select您特权以使此查询起作用。

所以:连接hr并运行

grant select on thong_tin_thue_phong to user_that_runs_that_pl/sql_block;

至于你得到的另一个错误:在dbms_output.put_line循环内的调用中,你正在引用,但是 - 它在循环的查询ds.soluong中不存在:它有,所以 - 使用它们。FORthangsokhach


此外,如果您创建了一个带IN参数的过程,则不会使用替换变量来请求它。程序是:

create or replace procedure thangmax3(par_nam IN number)
  as
begin
  dbms_output.put_line('DS 3 thang nhieu khach den o ksan nhat nam '|| par_nam);
  for ds in (select d.thang, 
                    d.sokhach 
             from (select extract (month from thoigiannhan) as thang,
                          COUNT(mathuephong) sokhach 
                   from HR.thong_tin_thue_phong 
                   where extract (year from thoigiantra) = par_nam 
                   group by extract (month from thoigiannhan)
                   order by COUNT(mathuephong) DESC
                  ) d 
             where rownum<=3
            )
  loop
    dbms_output.put_line('Thang: '||ds.thang||'     '||'So khach: '||ds.sokhach);
  end loop;
end;

你可以称之为

begin
  thangmax3(&nam);
end;
/

推荐阅读