如何自动化Oracle SQL开发人员每天一次将查询导出到CSV/XLSX

发布于 2025-02-11 02:50:12 字数 84 浏览 1 评论 0原文

我是SQL的新手,我的团队的任务是运行需要SQL数据的每日报告。是否有一种方法可以使用Oracle SQL开发人员每天将查询自动导出到CSV/Excel?

I'm fairly new to SQL and my team has been tasked with running a daily report which requires SQL data. Is there a way to use Oracle SQL Developer to automatically export a query to csv/excel on a daily basis?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱人如己 2025-02-18 02:50:12

程序Oracle SQL开发人员本身不会自动化它,但是有很多选择可以完成。

基本上,某些计算机需要具有可以唤醒并完成工作的调度程序。有些人使用Windows计划的任务,CRON作业或第三方程序使用自己的计算机为此。

但是,您的计算机将运行什么“任务”?该任务基本上是要连接到数据库,进行身份验证,发送查询文本,在内存中检索结果,然后将其导出为文件。很多人会为此使用Python,因为它可以处理所有这些步骤。

您会注意到,我们已经处于2个外部“事物”可能失败的情况。
(1)您的计算机,
(2)您的Python代码。

一个更简单的选择是Oracle数据库本身。是在始终运行的远程服务器上吗?如果是这样,您可能会利用这一点,并拥有 完成所有工作。

您正在查看几个步骤,但我认为它们更容易(尽管您可能必须从管理员那里获得权限)。

指定文件位置 - 请记住,这在服务器上,而不是您的计算机

create or replace directory csv_dir as '/destination/for/results';

创建存储过程 - 像SQL Logic的容器一样。

create or replace procedure write_file is
  file_handle UTL_FILE.file_type;
begin
  file_handle := utl_file.fopen('CSV_DIR', 'csv_filename.csv', 'w', 32767);
  for rws in (
    select * from t -- your query here
  ) loop
    utl_file.put_line(file_handle, 
      rws.c1 || ',' || rws.c2 || ',' || rws.c3 -- your columns here
    );
  end loop;
  utl_file.fclose(file_handle);
end write_file;

创建调度程序作业 - 运行存储过程。这是您的Oracle服务器的功能。

BEGIN
  DBMS_SCHEDULER.CREATE_JOB (
    job_name => 'EXPORT_CSV_J',
    job_type => 'PLSQL_BLOCK',
    job_action => 'begin write_file; end;',
    number_of_arguments => 0,
    start_date => NULL,
    repeat_interval => 'FREQ=DAILY',
    end_date => NULL,
    enabled => FALSE,
    auto_drop => FALSE);

  DBMS_SCHEDULER.SET_ATTRIBUTE( 
    name => 'EXPORT_CSV_J', 
    attribute => 'logging_level', 
    value => DBMS_SCHEDULER.LOGGING_RUNS);
  
  DBMS_SCHEDULER.enable(
    name => 'EXPORT_CSV_J');
END;

我从这个网站自从我自己做这件事已经有一段时间了。

如果由于某种原因,您的数据库实际上不是Oracle(我知道有些人使用Oracle SQL开发人员即使他们的实际数据库有所不同),那么步骤将是相似的,但是代码会有所不同。

The program Oracle SQL Developer itself won't automate it, but there are plenty of options to get it done.

Basically, some computer somewhere needs to have a scheduler that can wake it up and do the work. Some people use their own computers for this, using windows scheduled tasks, cron jobs, or 3rd party programs.

But what "tasks" will your computer run? The task is basically to connect to the database, authenticate, send the Query text, retrieve the results in memory, and then export them as a file. Lot's of people will use python for this, because it can handle all of those steps.

You'll notice that we're already at 2 external "things" that could fail.
(1) your computer,
(2) your python code.

A simpler option, is the oracle database itself. Is it on a remote server that's always running? If so, you might take advantage of that and have it do all the work.

You're looking at a few steps, but I think they are easier (you might have to get permissions from an admin, though).

Specify a file location - remember, this is on the server, not your computer

create or replace directory csv_dir as '/destination/for/results';

Create a stored procedure - acts like a container for your SQL logic.

create or replace procedure write_file is
  file_handle UTL_FILE.file_type;
begin
  file_handle := utl_file.fopen('CSV_DIR', 'csv_filename.csv', 'w', 32767);
  for rws in (
    select * from t -- your query here
  ) loop
    utl_file.put_line(file_handle, 
      rws.c1 || ',' || rws.c2 || ',' || rws.c3 -- your columns here
    );
  end loop;
  utl_file.fclose(file_handle);
end write_file;

Create a scheduler job - runs the stored procedure. This is a feature of your Oracle server.

BEGIN
  DBMS_SCHEDULER.CREATE_JOB (
    job_name => 'EXPORT_CSV_J',
    job_type => 'PLSQL_BLOCK',
    job_action => 'begin write_file; end;',
    number_of_arguments => 0,
    start_date => NULL,
    repeat_interval => 'FREQ=DAILY',
    end_date => NULL,
    enabled => FALSE,
    auto_drop => FALSE);

  DBMS_SCHEDULER.SET_ATTRIBUTE( 
    name => 'EXPORT_CSV_J', 
    attribute => 'logging_level', 
    value => DBMS_SCHEDULER.LOGGING_RUNS);
  
  DBMS_SCHEDULER.enable(
    name => 'EXPORT_CSV_J');
END;

I borrowed this code from this website since it has been a while since I did this myself.

If for some reason your database isn't actually Oracle (I know some people use Oracle SQL Developer even though their actual database is something different) then the steps will be similar but the code will be different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文