Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ORA-01031 with materialized view in stored procedure

193996Jul 9 2004 — edited Jul 9 2004
USER1 has created a materialized view (MV). The MV is owned by USER2. USER1 needs to query the MV from within a stored procedure. How do I avoid the ORA-01031?

Here's the illustration:

SQL> show user
USER is "USER1"

SQL> CREATE MATERIALIZED VIEW USER2.ROLL
2 BUILD IMMEDIATE
3 REFRESH COMPLETE WITH ROWID
4 ON DEMAND
5 AS
6 select * from user2.test_roll;

Materialized view created.

SQL> select status,owner,object_name,object_type where OBJECT_TYPE='MATERIALIZED VIEW';

STATUS OWNER OBJECT_NAME OBJECT_TYPE
------- ------------- ------------------- ------------------
VALID USER2 ROLL MATERIALIZED VIEW

SQL> select count(*) from user2.roll;

COUNT(*)
----------
959485

SQL> declare
2 n number;
3 begin
4 select count(*)
5 into n
6 from user2.roll
7 where rownum<100;
8 dbms_output.put_line(n);
9* end;
SQL> /
99

PL/SQL procedure successfully completed.

SQL> create procedure test_mv is
2 n number;
3 begin
4 select count(*)
5 into n
6 from as400_snpsht.roll
7 where rownum<100;
8 dbms_output.put_line(n);
9 end;
10 /

Warning: Procedure created with compilation errors.

SQL> select text from user_errors where name like 'TEST_MV';

TEXT
----------------------------------------------------------------
PL/SQL: ORA-01031: insufficient privileges
PL/SQL: SQL Statement ignored


So the point is:
* Ad hoc sql works on the MV
* Anonymous pl/sql works on the MV
* Stored procedure fails compilation with ora-01031

Any suggestions are appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 6 2004
Added on Jul 9 2004
7 comments
541 views