0

Need your or guidance on how I can execute a select on multiple databases provided in the list. the goal behind this code is to query multiple remote databases and insert the output in current database.

Need to db_link to be fetched from a list or table

insert into xxxx.DB_tracker value(SELECT d.name FROM v$database@**opXXX_du**);
3
  • Welcome to stackoverflow! Is your list fixed (i.e. doesn't change) or variable (and is stored in a table)? Commented May 27, 2020 at 5:30
  • DBA_DB_LINKS(db_link column) has all information about db links but you need select privilege on this table also select privilege on table v$database in all remote db's. Commented May 27, 2020 at 5:47
  • Thanks Folks. For your input. .. Littlefoot help worked. Commented Jun 24, 2020 at 5:32

1 Answer 1

2

Dynamic SQL.

Suppose that database links are stored in the link table:

SQL> select * From links;

LINK
---------
dbl_ora10
dbl_ora11
dbl_orcl

You'd then use a loop, create an insert statement and execute it. As I don't have those database links, I'm just displaying statements to the screen. You'd uncomment the execute immediate line.

SQL> set serveroutput on
SQL> declare
  2    l_str varchar2(200);
  3  begin
  4    for cur_r in (select link from links) loop
  5      l_str := 'insert into db_tracker ' ||
  6               'select name from v$database@' || cur_r.link;
  7      dbms_output.put_line(l_str);
  8
  9      -- execute immediate l_str;
 10    end loop;
 11  end;
 12  /
insert into db_tracker select name from v$database@dbl_ora10
insert into db_tracker select name from v$database@dbl_ora11
insert into db_tracker select name from v$database@dbl_orcl

PL/SQL procedure successfully completed.

SQL>

If you want to actually select name and display it on the screen, then you need the into clause. Something like this:

SQL> set serveroutput on
SQL>
SQL> declare
  2    l_name varchar2(30);
  3  begin
  4    for cur_r in (select link from links) loop
  5      execute immediate 'select name from v$database@' || cur_r.link
  6        into l_name;
  7      dbms_output.put_line(l_name);
  8    end loop;
  9  end;
 10  /
XE

PL/SQL procedure successfully completed.

SQL>
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome. thanks Little foot. This is the 1st time i posted question on Stakoverflow and 100% solution. thanks again ... it worked for me.
Mate if you dont mind can you help on how i can display the output for "select name from v$database@" or if I remove the insert and just want the select to be run displaying the output.
Let me not be that lazy. We need to declare the variable and the fetch the output to the variables. If there is any tip do let me know. Thanks for checking my post and helping
I edited my answer and added an example which shows how to do it. Have a look, please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.