Question

Is there a way, to select data only form the own schema, even if there is an public synonym?

something like: Select * from current_schema.Table1

more info:

I got a public synonym on table1 on schema1.
now I have a package(on schema2) that selects table1, I want to select table1 of schema2 not schema1.
My problem is, I dont what the user to change the identifier when he uses the package at his server.

edit
I see my question is not clear, what i wanted to know is is there a placeholder for my current schema?

at the moment i need to do this: Select * from schema2.Table1

and i want is something Like this : Select * from MySchema.Table1 or Select * from this.Table1 or Select * from current_schema.Table1

does something like this exists in oracle?

Was it helpful?

Solution

The scoping rules are quite clear. When the databse parses a query it looks for objects matching the identifiers in your statement in the following order of precedence:

  1. objects of that name in your schema
  2. private synonyms of that name (in your schema)
  3. public synonys of that name

But if you want to be clear certainly you can prefix your table references with the specific schema name. That is helpful in communicating your intent to others looking at your code.

Furthermore, if you have a table TABLE1 in your schema and there is a public synonym called TABLE1 pointing at a table in another schema which you want to query instead you must prefix your reference with that other schema.


"what i wanted to know is is there a placeholder for my current schema"

No, because it's not necessary. The default is always your current schema. That is, this statement ...

SQL>  select * from t23;

... will always select from T23 in your current schema, if it has a table (or a private synonym) with that name.

Note that it is possible to change the value of your current schema, with the ALTER SESSION command:

SQL>  alter session set current_schema=scott;

Now if you executed the previous select it would return results from SCOTT.T23 providing the SCOTT schema had such a table, and taht you had privileges on it. You can find out more about Oracle schemas in a blog piece I wrote a while back.


I was trying to understand what problem you were having, and I noticed that your scenario is one user executing a package owned by another user. Now, by default a package owned by SCHEMA2 will run against objects owned by SCHEMA2 and use the privileges on other objects granted to SCHEMA2.

But PL/SQL offers us the ability to change that: the AUTHID clause determines whether the package runs with the definer's privileges (that is the package owner) or invoker's privileges (the current user. So if SCHEMA2 defined their package with AUTHID CURRENT_USER when SCHEMA1 runs it the instance of TABLE2 will be the one in scope of SCHEMA1, which would be the one owned by SCHEMA1 or the one indicated by a public synonym.

Find out more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top