Pages

Wednesday, March 19, 2014

Oracle Views Issues. ORA-01031: insufficient privileges When creating a view.

=========================================
ORA-01031: insufficient privileges When creating a view
=========================================

Consider following scenario:
User USER_A wants to create view on objects owned by USER_B.

When doing
CREATE OR REPLACE VIEW MY_TABLE_B_VW
AS
SELECT * 
FROM USER_B.TABLE_B

There is an Oracle Error:
ORA-01031: insufficient privileges


====================
What to check
====================
A.
SELECT * FROM USER_B.TABLE_B;
Result is successful.

B. SELECT *
FROM ROLE_SYS_PRIVS
WHERE ROLE IN 
  (SELECT granted_role 
     FROM USER_ROLE_PRIVS
      AND username='USER_A')
AND privilege LIKE '%VIEW%' 

Result: USER_A got CREATE_VIEW privilege.

====================
Solution
====================
When creating a view on a table owned by another user, USER_A, must be granted explicit permission via GRANT SELECT ON TABLE_B By USER_B, and not via role.

As USER_B:
GRANT SELECT ON TABLE_B TO USER_A;

As USER_A:
CREATE OR REPLACE VIEW MY_TABLE_B_VW
AS
SELECT * 
FROM USER_B.TABLE_B

View created

No comments:

Post a Comment