When I have a sql statement like select * from table1
, it works great, but as soon as I put it into a function, I get:
ORA-00942: table or view does not exist
How to solve this?
Reference the Correct Schema You may be seeing the Ora-00942 error because you are referencing a table or view in a schema which you did not create but one that is in another schema. To correctly execute the query from another schema, you must reference the table by the schema name.
check the spelling of the table or view name. check that an existing table or view name exists. contact the DBA if the table needs to be created or if user or application privileges are required to access the table.
It means exactly what it says, the table or view you are executing your query on does not exist in your schema. To explain, whenever you execute a query which includes a table, view, synonym or a cluster which does not exist into the schema with which you are connected the SQL engine will show you this error.
Grant SELECT on all tables in a schema to a user Unfortunately, Oracle doesn't directly support this using a single SQL statement. To work around this, you can select all table names of a user (or a schema) and grant the SELECT object privilege on each table to a grantee.
There's a strong chance that the privileges to select from table1 have been granted to a role, and the role has been granted to you. Privileges granted to a role are not available to PL/SQL written by a user, even if the user has been granted the role.
You see this a lot for users that have been granted the dba role on objects owned by sys. A user with dba role will be able to, say, SELECT * from V$SESSION
, but will not be able to write a function that includes SELECT * FROM V$SESSION
.
The fix is to grant explicit permissions on the object in question to the user directly, for example, in the case above, the SYS user has to GRANT SELECT ON V_$SESSION TO MyUser;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With