如何在以下过程中正确引用数据库 link 名称
How can I properly reference the database link name in the below procedure
我在另一个论坛上问过一个类似的问题,但我没有完全理解上下文,所以我创建了一个更好的例子。有谁知道我如何在这里正确引用 link ?如果您对数据库进行硬编码,代码会将 .csv 文件发送到相关目录 link,但我想将其传递到变量中。
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;
/
一种选择是使用动态 SQL。如果是简单,没问题,但是- 动态游标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;
我在另一个论坛上问过一个类似的问题,但我没有完全理解上下文,所以我创建了一个更好的例子。有谁知道我如何在这里正确引用 link ?如果您对数据库进行硬编码,代码会将 .csv 文件发送到相关目录 link,但我想将其传递到变量中。
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;
/
一种选择是使用动态 SQL。如果是简单,没问题,但是- 动态游标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;