ORA-00942: table or view does not exist using iBatis - oracle

I am using MyBatis 3 to create a request (seen below) however, I am getting:
ORA-00942: table or view does not exist
where SYNONYM_A is a public Synonym for a table in another database...
I know the example doesn't really help, but the real question is, "Is there a special syntax for synonyms in Batis?" Has anyone done this, or failed, and can tell me, so I don't spend a great deal of effort, if it is not valid in Batis?
#Select("select * from SYNONYM_A where some_det_key in (SELECT DATA_KEY FROM SOME_PARENT_TABLE WHERE PARENT_KEY = 1234 AND (ATTACH_PARENT_FLG = 1 or ATTACH_PARENT_FLG is null) AND DATA_SRC = 'LV_SOME_DET') ORDER BY pair

Provide the select grant FROM the PARENT schema to the current schema once again for the table of which SYNONYM_A is a synonym of.
And is SYNONYM_A a synonym for a Table or a synonym of another SYNONYM in the PARENT schema?
In case it is, u need to provide grant from the ultimate base schema where the actual table is located once again 'WITH GRANT OPTION'

A public synonym does not mean that grants are not required from the schema in which the parent table lies(if the synonym and the table lies in two different schemas, that is). You need to clarify your concept of public synonyms :) .
But thats not the point here.
just do:
SELECT * SOME_TABLE"#"CONNECTION_TO_ANOTHER_DATABASE;
If this gives you ORA-XXXX: Table or view doesnot exist, then this is the cause.
There are two possibilities:
1.CONNECTION_TO_ANOTHER_DATABASE has got corrupted/doesnot exist.
You can check this by doing queries like :
Select sysdate from duals#CONNECTION_TO_ANOTHER_DATABASE;
Select * from user_objects#CONNECTION_TO_ANOTHER_DATABASE;
If this too gives you the same error then that's it, your CONNECTION_TO_ANOTHER_DATABASE is gone.
If this does not give any error and gives some valid o/p then:
2.Probably the "SOME_TABLE" at the remote database does not exist!

The answer is....
I was connected to the wrong database instance!! I had built extended AbstractRoutingDataSource, and was pulling the server name for the datasource from a primary database. I discovered that another individual had populated this table incorrectly, and pointed me to a database where the synonym did not exist.
Thank you for your responses, and your time.

Related

Oracle: why I can't select from VIEW using <SCHEMA_NAME>.<VIEW_NAME>?

There are some changes on the core product on which I'm working and some tables become now views and they are not working anymore because a view cannot be referenced with the schema name in front.
For example, the below will return an error: ORA-00942: table or view does not exist
select * from my_schema.my_view;
while a direct select from the view works fine
select * from my_view;
In case of a table, both scenarios above are working fine, is just the view that doesn't accept schema_name in front.
Why is that? Are there any decent workarounds?
EDIT: the selects are executed with my_schema user
Thanks all for your help, especially #mathguy.
Basically the problem was that my_view was in fact a public synonym for my_view_r which was the actual view and being public, you cannot call it using the schema name in front like I was trying. eg:
select * from my_schema.my_view;
Maybe it will be helpful for others that are facing this issue in the future, the workaround would be to create a private synonym to the same view (my_view_r) using the schema name like below:
create synonym my_schema.my_view for my_view_r;
This is the only way to call a synonym using the schema name.
It's a grant issue.
grant all on my_view to my_schema
Make sure your schema really is the owner by running:
Select * from all_objects where object_name = 'my_view';
I have a view stvytro with owner baninst1. There is a public synonym of the same name. The following both work:
select * from STVYTRO;
select * from baninst1.stvytro;

How to use synonym of a DBlink in Oracle?

I have created a synonym for a dblink.
create synonym dblink2 for dblink1
But when I query anything using the synonym instead of the dblink, I'm getting connection description for remote database not found error.
SELECT * FROM DUAL#DBLINK2
How do I query using the synonym?Edit: I know that it'll work if I create a view of the table using dblink. But my requirement is the above question.
Unfortunately creation of synonyms for dblinks is not supported. If you read the documentation on synonyms, you will find that the permitted objects for synonyms are only:
Use the CREATE SYNONYM statement to create a synonym, which is an
alternative name for a table, view, sequence, procedure, stored
function, package, materialized view, Java class schema object,
user-defined object type, or another synonym.
The reason why your second query fails is that the synomym you have created is not functioning correctly. It is not being validated properly at creation time, and you can create any sort of incorrect synonyms like that. To verify, just test the following statement:
create synonym dblink3 for no_object_with_this_name;
You will still get a response like this:
*Synonym DBLINK3 created.*
But of course nothing will work via this synonym.
I don't see the point in creating a synonym for the dblink itself. Ideally you create the synonym for the remote table using the dblink.
CREATE DATABASE LINK my_db_link CONNECT TO user IDENTIFIED BY passwd USING 'alias';
CREATE SYNONYM my_table FOR remote_table#my_db_link;
Now, you could query the remote table using the synonym:
SELECT * FROM my_table;
I'm trying to think of the business issue that gets solved by putting a synonym on a db_link, and the only thing I can think of is that you need to deploy constant code that will be selecting from some_Table#some_dblink, and although the table names are constant different users may be looking across different db_links. Or you just want to be able to swap which db_link you are operating across with a simple synonym repoint.
Here's the problem: it can't be done that way. db_link synonyms are not allowed.
Your only solution is to have the code instead reference the tables by synonyms, and set private synonyms to point across the correct db_link. That way your code continues to "Select from REMOTE_TABLE1" and you just can flip which DB_LINK you are getting that remote table from.
Is it a pain to have to set/reset 100+ private synonyms? Yep. But if it is something you need to do often then bundle up a procedure to do it for you where you pass in the db_link name and it cycles through and resets the synonyms for you.
While I understand that this question is 3+ years old, someone might be able to benefit from a different answer in the future.
Let's imagine that I have 4 databases, 2 for production and 2 for dev / testing.
Prod DBs: PRDAPP1DB1 and PRDAPP2DB1
Dev DBs: DEVAPP1DB1 and DEVAPP2DB1
The "APP2" databases are running procedures to extract and import data from the APP1 databases. In these procedures, there are various select statements, such as:
declare
iCount INTEGER;
begin
insert into tbl_impdata1
select sysdate, col1, col2, substr(col3,1,10), substr(col3,15,3)
from tbl1#dblink2; -- Where dblink2 points to DEVAPP1DB1
...
<more statements here>
...
EXCEPTION
<exception handling code here>
end;
Now that is okay for development but the dblink2 constantly needs to be changed to dblink1 when deploying the updated procedure to production.
As it was pointed out, synonyms cannot be used for this purpose.
But instead, create the db links with the same name, different connection string.
E.g. on production:
CREATE DATABASE LINK "MyDBLINK" USING 'PRDAPP1DB1';
And on dev:
CREATE DATABASE LINK "MyDBLINK" USING 'DEVAPP1DB1';
And then in the procedures, change all "#dblink1" and "#dblink2" to "#mydblink" and it all should be transparent from there.
If you are trying to have the DB link accessible for multiple schemas (users) the answer is to create a public db link
example:
CREATE PUBLIC DATABASE LINK dblink1 CONNECT TO user IDENTIFIED BY password USING 'tnsalias';
After that any schema can issue a:
SELECT * FROM TABLE#dblink1

Quering Oracle materialized view - column ambiguously defined

I have a trouble querying view in Oracle.
Here is the view (my_cool_view) definition:
SELECT *
FROM mview1 JOIN
mview2 USING(col_id)
where mview1 and mview2 is materialized views. I can't access to definition of this materialized views. I've tried this query:
select r.title from my_cool_view r;
and got ORA-00918: column ambiguously defined error. I got this error for subset of columns in my_cool_view.
If I run this query:
select * from my_cool_view;
all works fine.
You are asking about desc my_cool_view in comments, sorry I can't provide schema information because it's confidential. The only I can say it contains about 80 columns, and only one (col_id) don't cause above error.
Do you have any ideas what is the case and how to fix it?
Thanks in advance.
Sorry for misled you, the first version of question was too bad.
You probably have 2 columns called title from different tables/views that you've joined. If you select * then this will work as the duplicate title columns will get a different alias (like TITLE1). When you explicitly ask for one of the title columns the database needs to know which one you want and thus throws the error.
Just make sure that the title columns have a different alias:
select EMP.TITLE EMP_TITLE, DEPT.TITLE DEPT_TITLE
FROM ...

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

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?
Unqualified, BRUNCH refers to a different object than JOE.BRUNCH in your current session. You've got a couple of options to fix that.
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
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
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
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‘;
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;

Fluent Nhibernate Oracle Identifier Too Long - Alias Name Issue

I've tried to do that.
HasManyToMany<YechidotDoarInGroup>(x => x.Col_yig)
.Table("PigToYig")
.ChildKeyColumn("YIG_GROUP_RECID")
.ParentKeyColumn("PIG_GROUP_RECID");
but I've got:
ORA-00942: table or view does not exist
I am trying to establish HasManyToMany connection not by ID , but by
some other property .
First I've got - too long message. When I've tried to enter my own Table name as an alias , it's not recognized. What should I do?
The problem may well be this:
.Table("PigToYig")
Oracle object names are, by default, in UPPER case. However, Oracle applies names in double-quotes in the given case. In other words, if your table has the default naming you may need to pass in this instead ...
.Table("PIGTOYIG")
It depends how NHibernate converts those variables into SQL (I'm not familiar with NHibernate).
Define Table() method before all of your mapping declaration.
public EmployeeMap : ClassMap<Employee>
{
public EmployeeMap()
{
Table("EMPLOYEE");
// your declaration
Id(x => x.IdEmployee);
}
}
Cause: The table or view entered does
not exist, a synonym that is not
allowed here was used, or a view was
referenced where a table is required.
Existing user tables and views can be
listed by querying the data
dictionary. Certain privileges may be
required to access the table. If an
application returned this message, the
table the application tried to access
does not exist in the database, or the
application does not have access to
it.
Action:
Check each of the following:
* the spelling of the table or view name.
* that a view is not specified where a table is required.
* that an existing table or view name exists.
source ora-code.com

Resources