我必须创建包含三个过程的包,这些过程将将另一个函数的结果写入文件。函数get_cursor(…)返回sys_refcursor如下所示:
function get_cursor(
tabname in varchar2,
cols in array_t,
vals in array_t,
rels in array_t )
return sys_refcursor
is
cur sys_refcursor;
where_statement varchar2(1000);
tmp varchar2(1000);
begin
if cols.last != 0 then
where_statement := 'WHERE ' || cols(1) || ' ' || rels(1) || ' ';
if (rels(1) = 'in' or rels(1) = 'not in') then
where_statement := where_statement || trim(both '''' from vals(1));
elsif (utils.is_number(vals(1)) = 'N' and substr( vals(1), 1, 8 ) != 'TO_DATE(') then
where_statement := where_statement || '''' || vals(1) || '''';
else
where_statement := where_statement || vals(1);
end if;
for i in 2..cols.last
loop
where_statement := where_statement || ' AND ' || cols(i) || ' ' || rels(i) || ' ';
if (rels(i) = 'in' or rels(i) = 'not in') then
where_statement := where_statement || trim(both '''' from vals(i));
elsif ( utils.is_number(vals(i)) = 'N' and substr( vals(i), 1, 8 ) != 'TO_DATE(') then
where_statement := where_statement || '''' || vals(i) || '''';
else
where_statement := where_statement || vals(i);
end if;
end loop;
end if;
open cur for 'SELECT * FROM ' || tabname || ' ' || where_statement;
return cur;
end get_cursor;它是否正确工作并不重要,它返回一些内容,我必须在过程中将其写入文件,它将采用与get_cursor使用的+路径和文件名相同的参数:
procedure txt_export(
tabname in varchar2,
cols in array_t,
vals in array_t,
rels in array_t,
path in varchar2,
file_name in varchar2)
is
l_file utl_file.file_type;
tmp_file_name varchar2(4000) := file_name;
begin
if (tmp_file_name like '%.txt') then
l_file := utl_file.fopen(path, tmp_file_name, 'w');
elsif (tmp_file_name not like '%.txt') then
tmp_file_name := tmp_file_name || '.txt';
l_file := utl_file.fopen(path, tmp_file_name, 'w');
end if;
/*
run function get_cursor(tabname, cols, vals, rels) and write result to the file .txt
*/
utl_file.fclose(l_file);
end;请帮我解决那个问题。对不起我的英语:)
打招呼!
发布于 2013-07-30 08:51:49
棘手的是,函数返回弱引用游标;不能使用强引用游标,因为函数动态地组合查询的投影。因此,调用程序无法知道结果集的结构。这意味着您需要使用动态SQL查询结果集并计算出元数据。
幸运的是,您正在使用Oracle11g,因为Oracle引入了对方法4动态SQL的支持,它允许我们将Ref游标转换为DBMS_SQL游标。了解更多信息。
考虑到您已经知道如何用UTL_FILE打开和关闭文件,我想您应该知道如何用UTL_FILE.PUT()和UTL_FILE.NEW_LINE()写出列。
https://stackoverflow.com/questions/17940642
复制相似问题