将 PLSQL 过程的输出提取到笔记本电脑的本地驱动器
Extracting output from PLSQL procedure to local drive of my laptop
我在 sqldeveloper 中有一个数据库连接服务器 "server_dev"。
现在我想创建一个程序,其输出可以直接保存在 csv 文件中,以便稍后在我的笔记本电脑的本地驱动器中进行数据比较。
所以我尝试使用 UTL_FILE oracle 程序包,但是当我 运行 UTL_FILE 试图写入服务器文件 "server_dev" 而我无权访问该服务器,因此该命令不起作用。
例如:代码是:-
CREATE OR REPLACE PROCEDURE export_to_csv_test
IS
v_file UTL_FILE.file_type;
v_string VARCHAR2 (4000);
CURSOR c_contexts
IS
SELECT workspace_id,context_id from contexts where rownum<5;
BEGIN
v_file :=
UTL_FILE.fopen ('Z:\My_Project_knowledge\CSVDIR', 'empdata.csv','w',1000);
FOR cur IN c_contexts
`enter code here`LOOP
v_string :=
cur.workspace_id
|| ','
|| cur.context_id;
UTL_FILE.put_line (v_file, v_string);
END LOOP;
UTL_FILE.fclose (v_file);
END;
for calling it :-
BEGIN
export_to_csv_test;
END;
Error report:
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at "RAY_DEV07_OWNER.EXPORT_TO_CSV_TEST", line 20
ORA-06512: at line 3
29280. 00000 - "invalid directory path"
*Cause: A corresponding directory object does not exist.
*Action: Correct the directory object parameter, or create a corresponding
directory object with the CREATE DIRECTORY command.
所以,我对其进行了分析,发现我的 SQL 开发人员已连接到我本地计算机的服务器,并且由于它是我办公室的笔记本电脑,所以我无法更改它。
我可以使用任何其他方式我可以将存储过程的输出以文本或 Csv 文件的形式保存到我的本地驱动器吗?
要将文件写入本地计算机,您可以使用 dbms_output;例如在 SQLPlus 中:
SQL> set feedback off
SQL> set echo off
SQL> set serveroutput on
SQL> spool d:\spool.txt
SQL> begin
2 for i in (select level from dual connect by level <= 5) loop
3 dbms_output.put_line('Level ' || i.level);
4 end loop;
5 end;
6 /
将生成文件 d:\spool.txt:
Level 1
Level 2
Level 3
Level 4
Level 5
如果您可以直接从 table 或 table 函数 select,那么 SQL*Plus 12.2 的新 SET MARKUP CSV
选项将很有用.它不会对查询输出进行分页,而是生成 CSV。完整的语法是
SET MARKUP CSV {ON|OFF} [DELIMI[TER] character] [QUOTE {ON|OFF}]
如果使用 sqlplus -m
选项打开此模式,输出生成会更快。
它对于查询 JSON 类型也很有用。参见 https://blogs.oracle.com/opal/entry/fast_generation_of_csv_and
我在 sqldeveloper 中有一个数据库连接服务器 "server_dev"。
现在我想创建一个程序,其输出可以直接保存在 csv 文件中,以便稍后在我的笔记本电脑的本地驱动器中进行数据比较。
所以我尝试使用 UTL_FILE oracle 程序包,但是当我 运行 UTL_FILE 试图写入服务器文件 "server_dev" 而我无权访问该服务器,因此该命令不起作用。 例如:代码是:-
CREATE OR REPLACE PROCEDURE export_to_csv_test
IS
v_file UTL_FILE.file_type;
v_string VARCHAR2 (4000);
CURSOR c_contexts
IS
SELECT workspace_id,context_id from contexts where rownum<5;
BEGIN
v_file :=
UTL_FILE.fopen ('Z:\My_Project_knowledge\CSVDIR', 'empdata.csv','w',1000);
FOR cur IN c_contexts
`enter code here`LOOP
v_string :=
cur.workspace_id
|| ','
|| cur.context_id;
UTL_FILE.put_line (v_file, v_string);
END LOOP;
UTL_FILE.fclose (v_file);
END;
for calling it :-
BEGIN
export_to_csv_test;
END;
Error report:
ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at "RAY_DEV07_OWNER.EXPORT_TO_CSV_TEST", line 20
ORA-06512: at line 3
29280. 00000 - "invalid directory path"
*Cause: A corresponding directory object does not exist.
*Action: Correct the directory object parameter, or create a corresponding
directory object with the CREATE DIRECTORY command.
所以,我对其进行了分析,发现我的 SQL 开发人员已连接到我本地计算机的服务器,并且由于它是我办公室的笔记本电脑,所以我无法更改它。
我可以使用任何其他方式我可以将存储过程的输出以文本或 Csv 文件的形式保存到我的本地驱动器吗?
要将文件写入本地计算机,您可以使用 dbms_output;例如在 SQLPlus 中:
SQL> set feedback off
SQL> set echo off
SQL> set serveroutput on
SQL> spool d:\spool.txt
SQL> begin
2 for i in (select level from dual connect by level <= 5) loop
3 dbms_output.put_line('Level ' || i.level);
4 end loop;
5 end;
6 /
将生成文件 d:\spool.txt:
Level 1
Level 2
Level 3
Level 4
Level 5
如果您可以直接从 table 或 table 函数 select,那么 SQL*Plus 12.2 的新 SET MARKUP CSV
选项将很有用.它不会对查询输出进行分页,而是生成 CSV。完整的语法是
SET MARKUP CSV {ON|OFF} [DELIMI[TER] character] [QUOTE {ON|OFF}]
如果使用 sqlplus -m
选项打开此模式,输出生成会更快。
它对于查询 JSON 类型也很有用。参见 https://blogs.oracle.com/opal/entry/fast_generation_of_csv_and