在存储过程上调用dba表

时间:2013-10-13 04:31:34

标签: oracle stored-procedures oracle11g

我是Oracle数据库11g R2的新手,这是为了获取数据库表空间的信息。我运行下一个代码,如.sql

select  TABLESPACE_NAME,
INITIAL_EXTENT,
NEXT_EXTENT,
MIN_EXTENTS,
MAX_EXTENTS,
PCT_INCREASE,
STATUS,
CONTENTS
    from    dba_tablespaces
    order   by TABLESPACE_NAME ;

并且工作正常,但是当我尝试在存储过程中运行时:

CREATE OR REPLACE PROCEDURE TABLE_SPACE_INFO AS 
BEGIN
select  TABLESPACE_NAME,
INITIAL_EXTENT,
NEXT_EXTENT,
MIN_EXTENTS,
MAX_EXTENTS,
PCT_INCREASE,
STATUS,
CONTENTS
from    dba_tablespaces
order   by TABLESPACE_NAME ;
END TABLE_SPACE_INFO;

我收到了编译错误:

Error(3,3): PL/SQL: SQL Statement ignored
Error(11,7): PL/SQL: ORA-00942: table or view does not exist

解决方案是什么?为什么错?

3 个答案:

答案 0 :(得分:1)

我认为你没有足够的权限来访问dba_tablespaces。您应该在执行查询之前向用户授予权限或授予DBA角色。

答案 1 :(得分:0)

您可以使用包:

  create or replace PACKAGE YourPackge    AS   TYPE T_CURSOR IS REF CURSOR;
  PROCEDURE TABLE_SPACE_INFO(T_List OUT T_CURSOR);
  END;

  \

  create or replace   PACKAGE BODY  YourPackge as
  PROCEDURE TABLE_SPACE_INFO(T_List OUT T_CURSOR)
  IS
  BEGIN
    OPEN T_List FOR
  select  TABLESPACE_NAME,
  INITIAL_EXTENT,
  NEXT_EXTENT,
  MIN_EXTENTS,
  MAX_EXTENTS,
  PCT_INCREASE,
  STATUS,
  CONTENTS
  from    dba_tablespaces
  order   by TABLESPACE_NAME ;
  END TABLE_SPACE_INFO;
  END;

答案 2 :(得分:0)

您必须在dba_tablespace上显式授予自己的选择权才能在包中查看该表。存储过程或包不会继承角色的特定于对象的特权。如:

TEST.SQL:

create or replace procedure test as
  ret number;
BEGIN
  select count(*) into ret
    from dba_tablespaces;
end;
/

show errors

运行它:

SQL> @c:\bob\test

Warning: Procedure created with compilation errors.

Errors for PROCEDURE TEST:

LINE/COL ERROR
-------- -----------------------------------------------------------------
4/3      PL/SQL: SQL Statement ignored    
5/10     PL/SQL: ORA-00942: table or view does not exist

SQL> connect sys/syspwd@mydb as sysdba
Connected.
SQL> grant select on dba_tablespaces to myuser;

Grant succeeded.

SQL> connect myuser/mypwd@mydb
Connected.
SQL> @c:\bob\test

Procedure created.

No errors.
SQL>