對於寫某些資料庫監控或者編寫的過程、函式或包等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的學習,多動手,多讀官方文件,多總結總結確實很重要呀。
      技術沒有障礙,就看你肯不肯鑽 -:)