Oracle schema user cannot create table in procedure - oracle

I'm trying to create a temporary table in a procedure:
PROCEDURE pr_create_tmp_bp_table(fp_id NUMBER) IS
tbl_name CONSTANT VARCHAR2(20) := 'BP_TO_DELETE';
BEGIN
-- sanity checks removed for readablity
EXECUTE IMMEDIATE
'CREATE GLOBAL TEMPORARY TABLE ' || tbl_name || ' ' ||
'ON COMMIT PRESERVE ROWS AS ' ||
'SELECT * FROM infop_stammdaten.bp';
END;
If I copy the BEGIN.._END block to a SQL worksheet everything works fine. So I think the user has the right to create a temporary table. If I execute the procedure from the same SQL worksheet I get
Fehlerbericht -
ORA-01031: Nicht ausreichende Berechtigungen
ORA-06512: in "INFOP_STAMMDATEN.PA_DELETE_FP", Zeile 16
ORA-06512: in Zeile 6
01031. 00000 - "insufficient privileges"
*Cause: An attempt was made to perform a database operation without
the necessary privileges.
*Action: Ask your database administrator or designated security
administrator to grant you the necessary privileges
Line 16 (Zeile 16) point to the EXECUTE IMMEDIATE statment that creates the temporary table.
To me this look like the user does not have the same rights in the sql work sheet and when it executes a procedure, also the procedure is in it's own schema.

The answer to your immediate question is that you get ORA-01031: insufficient privileges because your user has the CREATE TABLE privilege granted through a role: the Oracle security model enforces a rule that we can't use privileges granted through roles in PL/SQL. So you need your DBA to grant the CREATE TABLE privilege to your user directly.
Or do you?
Because what you are trying to do does not make sense in Oracle. In Oracle global temporary tables are permanent structures; it's just the data in them which is temporary. So, the correct solution is to build the table once with a normal DDL script, like any other database object. Then you can just insert into the global temporary table as you need to.
You are not the first person on this site to make this mistake (read this pertinent thread). Often it's because people are coming from another database such as SQL Server which has a construct called "temporary table" which is actually different from Oracle's global temporary tables. If that's your scenario then you will be interested in an Oracle 18c new feature called Private Temporary Tables. These are exactly analogous to SQL Server temporary tables. Find out more.

Related

Referring to objects(tables) in other schema or user in Oracle procedure

Am accessing table data from other schema and inserting into current schema table in oracle procedure, but am unable to compile the procedure getting error "Error(5,27): PL/SQL: ORA-00942: table or view does not exist".
But where as when I select data from table(in other schema) directly am able to fetch the data but not in procedure. I have all required permissions as well.
Below is the procedure.
CREATE OR REPLACE PROCEDURE TEST_SCHEMA
IS
BEGIN
INSERT INTO table1
SELECT * FROM Other_User_Schema.Table2
COMMIT;
END;
It seems that you acquired privileges to select data from other_user_schema.table2 via role. If that's so, grant that privilege directly to your user because that won't work in named PL/SQL procedures.

Insufficient Privileges Create table

I am new with Sql Developer and I got this problem. I make connection but when I try to create table it shows me error:
ORA-01031: Insufficient Privileges.
I try to find answer but I did not succeed.
Please help
you or your dba should logon sys, and issue :
SQL> grant create any table to anonymous;
OR
SQL> grant create table to anonymous;
OR
SQL> grant resource to anonymous;
to have creating table privilege.
the difference between create table and create any table is that
if you have create table privilege then you can create a table in your
own schema.but if you have create any table system privilege then you
can create table in any schema.also to create an external table the
valid privilege is create any table if you use create table then it
will show an error.
I tried the chosen answer and it didn't work.
When you ask questions just wait until you recieve at least 3 answers and then give it green thick.
Try this instead(run it with sys or system user):
'GRANT RESOURCE to my_user; '
Source & already answered link:
Insufficient Privileges when creating tables in Oracle SQL Developer

ORA-00942 error while granting execution rights

I want to add partitions to an existing table. Therefore I followed this example.
When trying to:
grant execute on dbms_redefinition to USER
Oracle returns the following error:
Error starting at line 13 in command:
grant execute on dbms_redefinition to USER
Error report:
SQL Error: ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
Why does this happen? Is there a workaround for it?
First, you need to be logged in as SYSDBA to grant a user execute rights on that package.
Second, be aware that for dbms_redefinition to work the user also requires the following rights:
ALTER ANY TABLE
CREATE ANY TABLE
DROP ANY TABLE
LOCK ANY TABLE
SELECT ANY TABLE
And, depending on the target table you might also need:
CREATE ANY INDEX
CREATE ANY TRIGGER
Those are VERY powerful rights to be granted to a user, so you might want to revoke them after doing what needs to be done. Another good example for using this package is here

Stop SQL statements without schema qualifiers

How can I stop a SQL statement from running if it's missing a schema qualifier? Most of these issues are caught by a development process, but is there a way to stop the ones that slip through the cracks?
For example, this statement should work:
create table jheller.test_table(a number);
This statement should fail:
create table test_table(a number);
Most of these problems are easily caught during development. Usually a lack of privileges will cause an error like ORA-00942: table or view does not exist. Or if the statement runs successfully on the wrong schema it will cause an obvious error that will be caught during testing.
But inevitably some bad statements still make it into deployments that are promoted to the upper environments. This leads to broken deployments and invalid objects created in schemas like SYS. (We shouldn't be running so many deployments as SYS but that's beyond our control.)
It's not necessary to catch 100% of these issues. But catching 99.9% instead of 99% would make a significant difference.
SQL statements without schema qualifiers can be prevented by:
Creating a fake, empty schema on all databases.
Creating a database trigger to prevent creating objects in that schema.
Setting the session variable CURRENT_SCHEMA to that schema at the beginning of deployment scripts.
Installation - run once per database.
--Create a user. It won't be used so lock it and don't grant it any privileges.
create user schema_qualifier_required identified by "[SOME RANDOM PASSWORD HERE]";
alter user schema_qualifier_required account lock;
--Create trigger to prevent any other user from creating objects on it.
create or replace trigger schema_qualifier_required.no_objects_on_schema_qualifier
before ddl on database
/*
Purpose: SCHEMA_QUALIFIER_REQUIRED exists only to help prevent statements
without schema qualifiers. This trigger ensures no objects can be created in
the schema.
Run this command in a session to help ensure schema qualifiers are used:
alter session set current_schema=schema_qualifier_required;
To drop or modify the schema this trigger must be dropped like this:
alter system set "_system_trig_enabled"=false;
drop trigger schema_qualifier_required.no_objects_on_schema_qualifier
alter system set "_system_trig_enabled"=true;
*/
begin
if ora_dict_obj_owner = 'SCHEMA_QUALIFIER_REQUIRED' then
raise_application_error(-20000, 'You cannot create objects in this schema. '||
'Did you forget to use a schema qualifier in your statement?');
end if;
end;
/
Non-qualified statements initially work.
SQL> create table test1(a number);
Table created.
SQL> select * from test1;
no rows selected
ALTER SESSION to prevent future non-qualified statements from running.
SQL> alter session set current_schema=schema_qualifier_required;
Session altered.
Non-qualified statements no longer work.
SQL> create table test2(a number);
create table test2(a number)
*
ERROR at line 1:
ORA-00604: error occurred at recursive SQL level 1
ORA-20000: You cannot create objects in this schema. Did you forget to use a
schema qualifier in your statement?
ORA-06512: at line 3
SQL> select * from test1;
select * from test1
*
ERROR at line 1:
ORA-00942: table or view does not exist
I have not used this method in production yet. If anyone sees problems with this approach or knows of a better way please edit, comment, or add another answer.

Getting ORA-01031: insufficient privileges while querying a table instead of ORA-00942: table or view does not exist

When I'm querying a table in schema C from schema A, I'm getting ORA-01031: insufficient privileges and when I'm querying the same table from schema B, I'm getting ORA-00942: table or view does not exist. On the table neither of the schemas are having any privileges. Why am I getting different error messages in this case?
You may get ORA-01031: insufficient privileges instead of ORA-00942: table or view does not exist when you have at least one privilege on the table, but not the necessary privilege.
Create schemas
SQL> create user schemaA identified by schemaA;
User created.
SQL> create user schemaB identified by schemaB;
User created.
SQL> create user test_user identified by test_user;
User created.
SQL> grant connect to test_user;
Grant succeeded.
Create objects and privileges
It is unusual, but possible, to grant a schema a privilege like DELETE without granting SELECT.
SQL> create table schemaA.table1(a number);
Table created.
SQL> create table schemaB.table2(a number);
Table created.
SQL> grant delete on schemaB.table2 to test_user;
Grant succeeded.
Connect as TEST_USER and try to query the tables
This shows that having some privilege on the table changes the error message.
SQL> select * from schemaA.table1;
select * from schemaA.table1
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select * from schemaB.table2;
select * from schemaB.table2
*
ERROR at line 1:
ORA-01031: insufficient privileges
SQL>
ORA-01031: insufficient privileges happens when the object exists in the schema but do not have any access to that object.
ORA-00942: table or view does not exist happens when the object does not exist in the current schema. If the object exists in another schema, you need to access it using .. Still you can get insufficient privileges error if the owner has not given access to the calling schema.
for ORA-01031: insufficient privileges. Some of the more common causes are:
You tried to change an Oracle username or password without having the appropriate privileges.
You tried to perform an UPDATE to a table, but you only have SELECT access to the table.
You tried to start up an Oracle database using CONNECT INTERNAL.
You tried to install an Oracle database without having the appropriate privileges to the operating-system.
The option(s) to resolve this Oracle error are:
You can have the Oracle DBA grant you the appropriate privileges that you are missing.
You can have the Oracle DBA execute the operation for you.
If you are having trouble starting up Oracle, you may need to add the Oracle user to the dba group.
For ORA-00942: table or view does not exist. You tried to execute a SQL statement that references a table or view that either does not exist, that you do not have access to, or that belongs to another schema and you didn't reference the table by the schema name.
If this error occurred because the table or view does not exist, you will need to create the table or view.
You can check to see if the table exists in Oracle by executing the following SQL statement:
select *
from all_objects
where object_type in ('TABLE','VIEW')
and object_name = 'OBJECT_NAME';
For example, if you are looking for a suppliers table, you would execute:
select *
from all_objects
where object_type in ('TABLE','VIEW')
and object_name = 'SUPPLIERS';
OPTION #2
If this error occurred because you do not have access to the table or view, you will need to have the owner of the table/view, or a DBA grant you the appropriate privileges to this object.
OPTION #3
If this error occurred because the table/view belongs to another schema and you didn't reference the table by the schema name, you will need to rewrite your SQL to include the schema name.
For example, you may have executed the following SQL statement:
select *
from suppliers;
But the suppliers table is not owned by you, but rather, it is owned by a schema called app, you could fix your SQL as follows:
select *
from app.suppliers;
If you do not know what schema the suppliers table/view belongs to, you can execute the following SQL to find out:
select owner
from all_objects
where object_type in ('TABLE','VIEW')
and object_name = 'SUPPLIERS';
This will return the schema name who owns the suppliers table.
try to execute this on sql command line:
connect/ as sysdba;
create user b identified by "password";
grant all privileges to b;
and go create a new connection in SQL Developer;
do the same for schema 'c';
and grant privileges for schema 'a' too:
connect/ as sysdba;
grant all privileges to a;
this method fixed the problem for me.
In SQL Developer: Everything was working fine and I had all the permissions to login and there was no password change and I could click the table and see the data tab.
But when I run query (simple select statement) it was showing "ORA-01031: insufficient privileges" message.
The solution is simply disconnect the connection and reconnect.
Note: only doing Reconnect did not work for me.
SQL Developer Disconnect Snapshot
ORA-01031: insufficient privileges
Solution: Go to Your System User.
then Write This Code:
SQL> grant dba to UserName; //Put This username which user show this error message.
Grant succeeded.

Resources