Ramesh Soni
Ramesh Soni

Reputation: 16077

Referencing same table name in different schemas

I am facing problem with an Oracle Query in a .net 2.0 based windows application. I am using System.Data.OracleClient to connect to oracle database. Name of database is myDB. Below the the connection string I am using:

Data Source=(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)
(HOST =  172.16.0.24)(PORT = 1522)))(CONNECT_DATA =(SERVICE_NAME = ORCL)));
User ID=myDB;Password=myDB;Unicode=True

If I run the below query then it will given me wrong result (here wrong result means incorrect data. The data doesn't belongs to myDB):

SELECT ID, NAME  
FROM MyTempTable
WHERE ID IN (10780, 10760, 11890)

But if I append the database name along with it the it is giving correct result:

SELECT ID, NAME
FROM "myDB".MyTempTable
WHERE ID IN (10780, 10760, 11890)

My limitation is that I cannot append the database name as this is a generic application and can run with any database on run time. Please help.

Upvotes: 3

Views: 1805

Answers (5)

Gary Myers
Gary Myers

Reputation: 35401

Upon connecting to the database issue am

ALTER SESSION SET CURRENT_SCHEMA=abc;

where abc is the user that owns the tables.

Upvotes: 2

hamishmcn
hamishmcn

Reputation: 7981

To expand on what stjohnroe has said it looks like the reason you are getting different results is because two different tables with the same name exist on different schemas.
By adding the myDB username to the front of the query you now access the table with the data you are expecting. (Since you say the data doesn't belong on "myDB" this probably means the app/proc that is writing the data is writing to the wrong table too).
The resolution is:
1. If the table really doesn't belong on "myDB" then drop it for tidyness sake (now you may get 904 table not found errors when you run your code)
2. Create a synonym to the schema and table you really want to access (eg CREATE SYNONYM myTable FOR aschema.myTable;)
3. Don't forget to grant access rights from the schema that owns the table (eg: GRANT SELECT,INSERT,DELETE ON myTable TO myDB; (here myDB refers to the user/schema))

Upvotes: 1

stjohnroe
stjohnroe

Reputation: 3206

This looks like an issue with name resolution, try creating a public synonym on the table:

CREATE PUBLIC SYNONYM MyTempTable for MyTempTable;

Also, what exactly do you mean by wrong result, incorrect data, error message?


Edit: What is the name of the schema that the required table belongs to? It sounds like the table that you are trying to select from is in a different schema to the one that belongs to the user you are connecting as.

Upvotes: 2

Simon Munro
Simon Munro

Reputation: 5419

For starters, I would suggest that you use the .net data providers from Oracle - if at all possible. If you are starting off in a project it will be the best way to save yourself pain further down the line. You can get them from here

Upvotes: 1

skolima
skolima

Reputation: 32714

Try adding

CONNECT_DATA=(SID=myDB)(SERVICE_NAME=ORCL)

in the connection string.

Upvotes: 0

Related Questions