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

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.

Related

Oracle SQL Auto-updating Table From a Different Database

I have two tables from different database, db1.names and db2.names_rep. db1.names is constantly getting new data and I need it to reflect in db2.names_rep.
I've created a dblink connecting for db1 in db2 named dblink_db1.
CREATE DATABASE LINK dblink_db1
CONNECT TO user IDENTIFIED BY pass
USING '(DESCRIPTION=
(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))
(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=SID)))';
and a trigger that calls the db1.names table
create or replace trigger "names_trigger"
after insert or update or delete on "names#dblink_db1"
for each row
begin
if inserting then
insert into "names_rep" (name_id, student_names)
values (:NEW.name_id, :NEW.student_names);
elsif updating then
update "names_rep"
set name_id=:NEW.name_id, student_names=:NEW.student_names
where name_id=:NEW.name_id;
elsif deleting then
delete from "names_rep"
where name_id=:OLD.name_id;
end if;
end;
The dblink is working as i can invoke this query successfully in db2
select * from names#dblink_db1
I'm receiving an error that says this
Error report: ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
Lastly, I've read about Oracle Streams. Is that an alternative to Triggers? I'm hoping I don't have to specify each operation as to what is done in Trigger
You are trying to create a trigger in database db2 on a table in db1. I'm not sure that's possible.
I think the trigger needs to be created in db1, and to insert/update/delete rows over a link to db2.

Oracle schema user cannot create table in procedure

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.

How to write stored procedures using tables from another schema?

Is it necessary that table should be in HR user to call function and procedure of Oracle? When I run function from another user, it gives me a compilation error.
For my project,I want to run the function and procedure from another user. How can I implement it?
To write a function or procedure using a table in another schema the table owner needs to grant us the required privilege. For example:
SQL> conn hr/hr
SQL> grant select on employees to mousumi;
or ask a super user to do it for you:
SQL> conn dba_user/password
SQL> grant select on hr.employees to mousumi;
Privileges can be granted to roles but we cannot use those privileges to create procedures or views. For that purpose the privileges must be granted directly to us. Find out more.
When we reference that we need to include the schema name:
SQL> select * from hr.employees;
Alternatively we can create a synonym for it.
SQL> create synonym employee_table for hr.employees;
SQL> select * from employee_table;
The synonym is just a label: it can be anything we like providing it is unique within our schema (and conforms to Oracle naming conventions). Find out more.

Can we get "ORA-00942: table or view does not exist" error if the user is not able to log into the DB?

My application is giving the "ORA-00942: table or view does not exist" error when in prod environment. Locally it works just fine(when tested locally I am accessing staging DB). On checking the DB in prod I found tables to be there but we have still not populated the values.
To answer your question, no. You cannot receive an ORA-00942 if you are not even connected to the database.
To address your further comments it is probably a matter of properly identifying the table and schema it is in.
conn system/manager;
grant select on hr.employees to scott;
conn scott/scott;
select * from employees;
ORA-00942: table or view does not exist
select * from hr.employees;
<return data>
Alternatively you can create a synonym for the table in the scott schema:
create or replace synonym scott.employees for hr.employees;
This will allow the failed query to succeed as the scott schema has an object called employees in its scope.

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