分几个步骤
1,创建输出路径,比如你要在c盘test目录下输出,你就先要建立好这个test路径
2,sqlplus下以sysdba登录,执行以下语句
3,create or replace directory TMP as 'c:\test';
4,grant read,write on directory TMP to 你要生成文件的用户;
5,alter system set utl_file_dir='c:\test' scope=spfile;
以上步骤执行完,需要重启数据库
6,最重要的一步开始了,创建存储过程
create or replace PROCEDURE SP_OUTPUT
is
file_handle utl_file.file_type;
Write_content VARCHAR2(1024);
Write_file_name VARCHAR2(50);
v_id int;
v_form varchar2(10);
cursor cur_sp_out
is
select id,form from a;
begin
open cur_sp_out;
loop
fetch cur_sp_out into v_id,v_form;
耐森 exit when cur_sp_out%notfound;
write_file_name := to_char(SYSDATE,'MMDD')||'.txt';
file_handle := utl_file.fopen('TMP',write_file_name,'a');
write_content := v_id||' '||v_form;
--write file
IF utl_file.is_open(file_handle) THEN
utl_file.put_line(file_handle,write_content);
END IF;
--close file
utl_file.fclose(file_handle);
end loop;
close cur_sp_out;
end;
create table a(id int,
form varchar2(10));
insert into a values (1,'你好');
insert into a values (2,'你很好');
begin
SP_OUTPUT;
end;