Wednesday, 14 September 2011

Can one use synonyms with PLSQL packages

Question:

Steven, I would like to know if you can use synonyms with PL/SQL packages—such as synonyms named the same as a table in another schema so you don't have to copy data for testing purposes.

Answer:

Richard, you certainly can reference objects inside PL/SQL programs, including packages, that are actually synonyms pointing to underlying database objects.
And if you define your program to use "invoker rights" with the AUTHID CURRENT_USER clause, then when the program is run, that synonym will be resolved according to the privileges of the caller of the program.
In this manner, a single program can be run against tables in different schemas, without making any changes to your program. I used AUTHID CURRENT_USER in the tabcount function presented elsewhere in this Q&A:
CREATE OR REPLACE FUNCTION tabcount (
tab IN VARCHAR2, whr IN VARCHAR2 := NULL)
RETURN PLS_INTEGER
AUTHID CURRENT_USER

No comments:

Post a Comment