codeblocks如何让输出结果 空格_如何把sql语句结果输出到excel

472fc561c5ec8a5311a44359727d6257.png

如果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');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值