ORA-00942: Can select from “schema.table” but not “table”?

筅森魡賤 提交于 2020-01-28 09:58:20

问题


I experienced an ORA-00942 ("table or view does not exist") when executing

select * from brunch

However, no such problem when executing

select * from joe.brunch

May i know what is the issue here?


回答1:


Unqualified, BRUNCH refers to a different object than JOE.BRUNCH in your current session. You've got a couple of options to fix that.

  1. Create a public synonym. This will allow any user that has privileges on the JOE.BRUNCH table to access it by querying BRUNCH

    CREATE PUBLIC SYNONYM brunch FOR joe.brunch

  2. Create a private synonym. This will allow just the current user to access the JOE.BRUNCH table by querying BRUNCH

    CREATE SYNONYM brunch FOR joe.brunch

  3. Change the current schema for the current session to JOE. This will cause all unqualified references in the current session to resolve to the JOE schema rather than to the current user's schema

    ALTER SESSION SET current_schema = JOE




回答2:


There are several possible causes

1) there is more than one object (table,view, procedure, etc) called brunch. Oracle does not know which one you are referring to.

2) most likely cause: the table exists in the joe schema but you are connecting as another user who has not been granted select on the joe.brunch object

Try

Grant select on joe.brunch to your_user

and try this and see how many objects match the name brunch

select * from all_objects where object_type in (‘TABLE’,'VIEW’) and object_name = ‘brunch‘;




回答3:


I found that the table I was referencing (Flyway's schema_version table), was created with double quotes... and thus needed double quotes wherever it was referenced.

Here's what Oracle says:

A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you must use the double quotation marks whenever you refer to that object.

In practice, these worked:

SELECT * FROM MYSCHEMA."schema_version";
SELECT * FROM "MYSCHEMA"."schema_version";

When this didn't (-> ORA-00942: table or view does not exist):

SELECT * FROM MYSCHEMA.schema_version;


来源:https://stackoverflow.com/questions/7591569/ora-00942-can-select-from-schema-table-but-not-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!