我在另一个论坛上提出了类似的问题,但是我并没有完全理解上下文,因此我创建了一个更好的示例。有人知道我如何在这里正确引用链接吗?如果您对数据库链接进行硬编码,则代码会将.csv文件发送到相关目录,但是我想将其传递给变量。
CREATE OR REPLACE PROCEDURE WRITE_EST_SIZE
IS
file_handle UTL_FILE.file_type;
v_link_name VARCHAR2 (10);
v_csv_name VARCHAR2 (40);
BEGIN
v_csv_name := 'file_name';
--Table contains the name of the database link
SELECT link_name INTO v_link_name FROM link_name;
file_handle :=
UTL_FILE.fopen ('ESTIMATES_CSV',
v_csv_name || '.csv',
'w',
32767);
UTL_FILE.put_line (file_handle,
'OWNER,SEGMENT_NAME,SEGMENT_TYPE,SUM_BYTES');
FOR rws
IN ( SELECT s.owner,
s.segment_name,
'T' AS FLAG,
SUM (s.bytes) / 1024 / 1024 AS SUM_BYTES
FROM DBA_SEGMENTS@LINK_TO_DEV s
--Actually, I want THIS below to work
--from DBA_SEGMENTS@v_link_name s
WHERE s.owner =
(SELECT DISTINCT schema_name FROM target_schema)
AND s.segment_name IN (SELECT table_name FROM ref_table)
AND s.segment_type LIKE '%TABLE%'
GROUP BY s.owner, s.segment_name)
LOOP
UTL_FILE.put_line (
file_handle,
rws.OWNER
|| ','
|| rws.SEGMENT_NAME
|| ','
|| rws.FLAG
|| ','
|| rws.SUM_BYTES);
END LOOP;
UTL_FILE.fclose (file_handle);
END WRITE_EST_SIZE;
/
答案 0 :(得分:2)
一种选择是使用动态SQL。如果它是简单的东西,那没问题,但是-一个 dynamic 游标FOR
循环了吗?不,谢谢。
另一种选择是创建一个视图,作为UNION
的视图,例如您可能正在使用的所有数据库链接上的选择。
create or replace view v_dba_segments as
select 'db_link_1' link, s.owner, s.segment_name, ...
from dba_segments@db_link_1
union all
select 'db_link_2' link, s.owner, s.segment_name, ...
from dba_segments@db_link_2
union all
...
然后您在上述过程中将其引用为
for rws in (select owner, segment_name, ...
from v_dba_segments --> view
where link = v_link_name --> this is where your variable comes into the game
and ...
)
loop
...
end loop;