ORA-00942: table or view does not exist

來源:互聯網
上載者:User

      在過程,包,函數,觸發器中調用Oracle相關動態效能檢視時,需要授予適當的許可權,否則會收到表和視圖不存在的錯誤提示。即使你可以單獨查詢這些視圖。因為動態效能檢視依賴於底層表,無法直接對其授予許可權。下面就是這個現象相關的例子。

 

1、程序呼叫動態視圖無法成功編譯的樣本

SQL> select * from v$version where rownum<2;BANNER----------------------------------------------------------------Oracle Database 10g Release 10.2.0.5.0 - 64bit ProductionSQL> show user;USER is "GX_ADM"SQL> CREATE OR REPLACE PROCEDURE tst  2  AS  3      v_usr   VARCHAR2(30);  4  BEGIN  5  SELECT username INTO v_usr FROM v$process WHERE ROWNUM < 2;  6  DBMS_OUTPUT.put_line ('Username is ' || v_usr);  7  END;  8  /Warning: Procedure created with compilation errors.SQL> show errors;Errors for PROCEDURE TST:LINE/COL ERROR-------- -----------------------------------------------------------------5/1      PL/SQL: SQL Statement ignored5/33     PL/SQL: ORA-00942: table or view does not exist

2、分析與解決

--根據上面提示的錯誤資訊,咋一看就是表和視圖不存在--而實際上動態效能檢視是一個同義字,並非真正的視圖,下面嘗試使用sys帳戶對其賦予許可權到所需使用者--收到了ORA-02030錯誤資訊,也就是說只能對固定的表和視圖進行許可權賦予SQL> conn / as sysdbaConnected.SQL> grant select on v$process to gx_adm;grant select on v$process to gx_adm                *ERROR at line 1:ORA-02030: can only select from fixed tables/viewsSQL> conn gx_adm/xxx  -->使用gx_adm使用者串連資料庫Error accessing PRODUCT_USER_PROFILEWarning:  Product user profile information not loaded!You may need to run PUPBLD.SQL as SYSTEMConnected.--使用者本身是可以訪問該動態效能檢視的SQL> select username FROM v$process WHERE ROWNUM < 2 and username is not null;USERNAME---------------oracleSQL> conn / as sysdbaConnected.--Author : Leshami--Blog   : http://blog.csdn.net/leshami--基於真實的視圖授予許可權SQL> grant select on v_$process to gx_adm;Grant succeeded.--下面再次編譯正常gx_adm@CNMMBO> alter procedure tst compile;Procedure altered.--我們也可以通過執行計畫來查看底層訪問對象為X$KSUPR,這也就是為什麼前面授權失敗的原因SQL> set autot trace exp;SQL> select username FROM v$process WHERE ROWNUM < 2 and username is not null;Execution Plan----------------------------------------------------------------------------------------------------------------------------| Id  | Operation         | Name    | Rows  | Bytes | Cost (%CPU)|------------------------------------------------------------------|   0 | SELECT STATEMENT  |         |     1 |    35 |     0   (0)||   1 |  COUNT STOPKEY    |         |       |       |            ||   2 |   FIXED TABLE FULL| X$KSUPR |     1 |    35 |     0   (0)|------------------------------------------------------------------

3、Metalink文章(Doc ID 1062335.6)
ORA-942 when select from any v$view within stored PL/SQL procedure (Doc ID 1062335.6)
Problem Description:
~~~~~~~~~~~~~~~~~~~~
You are selecting from a system view, such as V$SESSION, from within a PL/SQL
stored procedure and you receive an ORA-00942 error.

    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.


Problem Explanation:
~~~~~~~~~~~~~~~~~~~~
The ORA-00942 is produced because the privilege to use the V$ views has been
granted to the user via a role, roles are not in effect within stored PL/SQL procedures.


Problem References:
~~~~~~~~~~~~~~~~~~~
Oracle7 Server Application Developer's Guide

 
Search Words:
~~~~~~~~~~~~~
ORA-942

Solution Description:
~~~~~~~~~~~~~~~~~~~~~

Grant the owner of the stored procedure select directly on the needed V$ view.
(Remember that the grant must be made on the actual table or view name, not the synonym):

SQL> GRANT SELECT on V_$SESSION to <user_name>;


Solution Explanation:
~~~~~~~~~~~~~~~~~~~~~
Granting the owner of the PL/SQL stored procedure select directly on the required
V$ view will allow the select to complete successfully.

 

   

更多參考

有關Oracle RAC請參考
     使用crs_setperm修改RAC資源的所有者及許可權
     使用crs_profile管理RAC資源設定檔
     RAC 資料庫的啟動與關閉
     再說 Oracle RAC services
     Services in Oracle Database 10g
     Migrate datbase from single instance to Oracle RAC
     Oracle RAC 串連到指定執行個體
     Oracle RAC 負載平衡測試(結合伺服器端與用戶端)
     Oracle RAC 伺服器端串連負載平衡(Load Balance)
     Oracle RAC 用戶端串連負載平衡(Load Balance)
     ORACLE RAC 下非預設連接埠監聽配置(listener.ora tnsnames.ora)
     ORACLE RAC 監聽配置 (listener.ora tnsnames.ora)
     配置 RAC 負載平衡與容錯移轉
     CRS-1006 , CRS-0215 故障一例 
     基於Linux (RHEL 5.5) 安裝Oracle 10g RAC
     使用 runcluvfy 校正Oracle RAC安裝環境

有關Oracle 網路設定相關基礎以及概念性的問題請參考:
     配置非預設連接埠的動態服務註冊
     配置sqlnet.ora限制IP訪問Oracle
     Oracle 監聽器日誌配置與管理
     設定 Oracle 監聽器密碼(LISTENER)
     配置ORACLE 用戶端串連到資料庫

有關基於使用者管理的備份和備份恢複的概念請參考
     Oracle 冷備份
     Oracle 熱備份
     Oracle 備份恢複概念
     Oracle 執行個體恢複
     Oracle 基於使用者管理恢複的處理
     SYSTEM 資料表空間管理及備份恢複
     SYSAUX資料表空間管理及恢複
     Oracle 基於備份控制檔案的恢複(unsing backup controlfile)

有關RMAN的備份恢複與管理請參考
     RMAN 概述及其體繫結構
     RMAN 配置、監控與管理
     RMAN 備份詳解
     RMAN 還原與恢複
     RMAN catalog 的建立和使用
     基於catalog 建立RMAN儲存指令碼
     基於catalog 的RMAN 備份與恢複
     RMAN 備份路徑困惑
     使用RMAN實現異機備份恢複(WIN平台)
     使用RMAN遷移檔案系統資料庫到ASM
     linux 下RMAN備份shell指令碼
     使用RMAN遷移資料庫到異機

有關ORACLE體繫結構請參考
     Oracle 資料表空間與資料檔案
     Oracle 密碼檔案
     Oracle 參數檔案
     Oracle 聯機重做記錄檔(ONLINE LOG FILE)
     Oracle 控制檔案(CONTROLFILE)
     Oracle 歸檔日誌
     Oracle 復原(ROLLBACK)和撤銷(UNDO)
     Oracle 資料庫執行個體啟動關閉過程
     Oracle 10g SGA 的自動化管理
     Oracle 執行個體和Oracle資料庫(Oracle體繫結構) 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.