I'm trying to run a set of queries over a DBLink that is determined at runtime (from configuration or other input). I have seen many examples suggesting to use dynamic SQL and EXECUTE IMMEDIATE. I would like to try and avoid this at all costs, since my queries are lengthy and likely to change so placing them in a string would make maintenance much more difficult.
I tried having my queries reference a synonym and then just updating the synonym prior to executing the query:
EXECUTE IMMEDIATE 'CREATE OR REPLACE SYNONYM my_tbl_s FOR my_tbl_t' || p_dblink_name;
SELECT * FROM my_tbl_s;
This works, however I want to use this in a package, so when I execute the create synonym the package is invalidated. I have also tried separating my code into different packages and recompling after the synonym update:
EXECUTE IMMEDIATE 'ALTER PACKAGE my_pck COMPILE';
However, the original calling package is still invalidated. Can anyone think of a way to achieve dynamic DB Links without using dynamic SQL?