现在的位置: 首页 > 综合 > 正文

PL/SQL中ORA-00942错的进一步研究

2013年01月03日 ⁄ 综合 ⁄ 共 2473字 ⁄ 字号 评论关闭

对于写某些数据库监控或者编写的过程、函数或包等PL/SQL的对象,如果其中包含ORACLE内部的动态性能试图(DYNAMIC PERFORMANCE VIEW)和数据字典时,常常会遇到如下错误:
    

Error: PL/SQL: ORA-00942: table or view does not exist

      查看一下错误原因:

Cause: The table or view entered does not exist, a synonym that is not allowed here was used, or a view was referenced where a table is required. Existing user tables and views can be listed by querying the data dictionary. Certain privileges may be required
to access the table. If an application returned this message, the table the application tried to access does not exist in the database, or the application does not have access to it.

Action: Check each of the following:

the spelling of the table or view name.

that a view is not specified where a table is required.

that an existing table or view name exists.

Contact the database administrator if the table needs to be created or if user or application privileges are required to access the table.

Also, if attempting to access a table or view in another schema, make certain the correct schema is referenced and that access to the object is granted.

  
      但是大家往往都非常愕然,奇怪呀,明明在SQL/PLUS中是可以直接查询的呀,并且返回了结果。但是为什么在编译过程、函数或者包时有错误出现呢?
      其实不然,只能说明我们当前的用户还没有足够的权限。
      那好,就直接授权,使得某个用户可以有确定的查询权限:
    

SQL> grant select on dba_tablespaces to xxx;

Grant succeeded.

SQL> grant select on v$session to xxx;
grant select on v$session to asg
                *
ERROR at line 1:
ORA-02030: can only select from fixed tables/views

     
      进行上述操作,可以看到数据字典(DATA DICTIONARY)可以直接授权,而使用了。但是DYNAMIC PERFORMANCE VIEW却不可以,并且报了ORA-02030的错误。
    

can only select from fixed tables/views

Cause: An attempt is being made to perform an operation other than a retrieval from a fixed table/view.

Action: You may only select rows from fixed tables/views.

      看到了,对于V$SESSION我的授权是有问题的!对,也就是说V$根本不是个VIEWS(如果你说根本不是TABLES,后面的论述就可以不用看了 -:)) 。
      看看ORACLE给出的说明:
     

V$ Views

The actual dynamic performance views are identified by the prefix V_$.
Public synonyms for these views have the prefix
V$. Database administrators and other users should access only the
V$
objects, not the V_$ objects.

  
      很显然,V$可以说是个“同义词”!所以会出现授权的错误。
      那么该如何授权呢?对,按照ORACLE给出的规则,按图索骥:

SQL> grant select on v_$session to XXX;

Grant succeeded.

      可以看到授权成功,为了编译成功,我们还要对编写的程序脚本进行轻微的改动,什么?对,就是将V$SESSION改写成SYS.V_$SESSION即可。
      接下来,再想想,如果我们编写的内容,涉及比较多的DATA DICTIONARY或者是DYNAMIC PERFORMANCE VIEW,我们一个一个授权岂不是很麻烦?
      我们可以通过ORACLE预先定义的角色,一次性完成授权:
     

SQL> grant select any  dictionary to xxx;

Grant succeeded.

     这样,我就可以直接使用DATA DICTIONARY和DYNAMIC PERFORMANCE VIEW。但是,一定要注意,这样的授权对于数据库的安全性来讲是不利的。
     ------------------------
      从ORA-00942开始的问题,我们将问题引申到DYNAMIC PERFORMANCE VIEW和ORACLE SECURITY等方面,确实很有意思。所以说,对于ORACLE的学习,多动手,多读官方文档,多总结总结确实很重要呀。
      技术没有障碍,就看你肯不肯钻 -:)

 

原文地址:http://miracle.blog.51cto.com/255044/93450/

 

抱歉!评论已关闭.