欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  新闻

plsql导入excel数据(plsql命令窗口导入sql文件)

程序员文章站 2022-07-18 11:42:40
如果sql语句的结果太大,通过plsql developer无法显示所有的结果,这个时候,我们可以通过一段代码来完成,下面是一个例子:把select last_name, salary, depart...

如果sql语句的结果太大,通过plsql developer无法显示所有的结果,这个时候,我们可以通过一段代码来完成,下面是一个例子:

select last_name, salary, department_id
from employees
order by department_id
的结果显示到excel
下面是具体例子:
create or replace procedure out_excel(dir in varchar2,
filename in varchar2) is
file utl_file.file_type;
cursor empc is
select last_name, salary, department_id
from employees
order by department_id;
begin
file := utl_file.fopen(dir, filename, ‘w’);
utl_file.put_line(file, ‘report: generated on ‘ || sysdate);
utl_file.new_line(file);
utl_file.put_line(file,
‘department_id’ || chr(9) || ‘name’ || chr(9) ||
‘salary’);
for emp_rec in empc loop
utl_file.put_line(file,
nvl(emp_rec.department_id, ”) || chr(9) ||
emp_rec.last_name || chr(9) || emp_rec.salary);
end loop;
utl_file.put_line(file, ‘*** end of report ***’);
utl_file.fclose(file);
exception
when
utl_file.invalid_filehandle then
raise_application_error(-20001, ‘invalid file.’);
when utl_file.write_error then
raise_application_error(-20002, ‘unable to write to file’);
when
utl_file.invalid_operation then
raise_application_error(-20003, ‘file operate invalid’);
end out_excel;
execute sal_status(dir =>’dir_file’,filename => ‘outexcel.xls’);