没用谁能帮我解决这个问题?

时间:2020-04-11 17:59:39

标签: oracle oracle11g

我创建了此过程,但是它说“ 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;

1 个答案:

答案 0 :(得分:0)

然后,代码中所涉及的唯一“真实”表就是

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,但是-在FOR循环的查询中不存在:它有{ {1}}和thang,请使用它们。


此外,如果您使用sokhach参数创建了一个过程,则不会使用替换变量来询问。程序将是:

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;