Thanks for the question, Francisco.
Asked: February 15, 2012 - 11:19 am UTC
Last updated: July 11, 2017 - 3:03 pm UTC
Version: 11.2.0.2
Viewed 10K+ times! This question is
You Asked
Hi Tom,
Hope everything is O.K. for you ......
You know, I am extracting segment_name information for several databases and I am inserting information in a repository table.
I am using next cursor to look for every database:
FOR x in (SELECT apl.id_aplicacion, app.nombre_aplicacion, app.db_link, ser.id_servidor, db.usuario, db.password, apl.tipo_database, apl.id_localidad
FROM aplicaciones apl, productos prod, database_links db, servidores ser, app_db_links app
WHERE apl.id_producto = prod.id_producto
AND apl.id_aplicacion = db.id_aplicacion
AND apl.id_aplicacion = app.id_aplicacion
AND apl.nombre_aplicacion = app.nombre_aplicacion
AND apl.id_servidor = ser.id_servidor
--AND apl.nombre_aplicacion = db.nombre_aplicacion
AND (apl.descripcion like 'Database%'
OR apl.descripcion like 'Base%')
AND (prod.nombre_producto like 'Database%'
OR prod.nombre_producto like 'Ebusiness%')
AND apl.tipo_database = 'PRODUCCION'
AND app.db_link = 'PRODMET')
LOOP
....
FOR j in (SELECT owner, segment_name, segment_type, tablespace_name, nvl(partition_name, 'NA') partition
FROM dba_segments@'||x.db_link||'
WHERE segment_type in ('TABLE','TABLE PARTITION')
AND owner not in ('SYS','SYSTEM'))
I want to use dba_segments@x.db_link in the second cursor dinamically (which is coming from the first one).
Do you know how can I use table_name (db_link) dinamically?
I really appreciate your help Sir.
Thank you very much.
Kind regards,
Francisco
and Tom said...
You will have to use dynamic SQL for this. I recommend this approach:
ops$tkyte%ORA11GR2> declare
2 cursor template_cursor is SELECT owner, segment_name, segment_type, tablespace_name, nvl(partition_name, 'NA') partition FROM dba_segments;
3
4 l_rec template_cursor%rowtype;
5 l_cursor sys_refcursor;
6 l_dblink varchar2(30) := 'loopback@ora11gr2';
7 begin
8
9 open l_cursor for q'|
10 SELECT owner, segment_name, segment_type, tablespace_name, nvl(partition_name, 'NA') partition
11 FROM dba_segments@|' || l_dblink || q'|
12 WHERE segment_type in ('TABLE','TABLE PARTITION')
13 AND owner not in ('SYS','SYSTEM')|';
14 loop
15 fetch l_cursor into l_rec;
16 exit when l_cursor%notfound;
17 -- process l_rec --
18 end loop;
19 close l_cursor;
20 end;
21 /
PL/SQL procedure successfully completed.
Rating
(5 ratings)
Is this answer out of date? If it is, please let us know via a Comment