Skip to Main Content
  • Questions
  • Is there a possibility to use db_link dynamically without using cursor and execute immediate?

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Cherry.

Asked: July 11, 2017 - 1:39 am UTC

Last updated: July 11, 2017 - 4:48 pm UTC

Version: 12.1.0

Viewed 1000+ times

You Asked

Hi,
I would like to know if I am able to implement db_link dynamically without using cursor or execute immediate? I have 2 tables stored in different location which are accessible via db_link. These 2 tables are identical in structure and the data columns that I would like to select are identical, the only difference is the db_link. For example below:

If <condition A is true> then
 select column A, B, C from tableA@dblink123;
else
 select column A, B, C from tableA@dblink456;
end if;

and Chris said...

Do you mean can you assign the DB link name to a variable and use that in your SQL (or similar)?

If so, the answer is no. Just like with table names, you have to use dynamic SQL if you want to use a different DB link in the "same" SQL.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

perhaps WHERE clause with a UNION ALL

steve, July 11, 2017 - 3:20 pm UTC

you could try:

select column A, B, C from tableA@dblink123
where <condition A is true>
UNION ALL
select column A, B, C from tableA@dblink456
where <condition A is false>
;
Chris Saxon
July 11, 2017 - 4:48 pm UTC

Yeah, that could work...