SQL query results to CSV with PL/SQL

In this post I’ll show you how to get Oracle SQL query results to CSV format into a CLOB only by calling stored procedures. This CLOB can be stored later in a table row or in a file using UTL_FILE.

There are two stored procedures:

  • The first one, cursor_to_csv, takes a cursor as parameter
  • The second one, query_to_csv, takes a VARCHAR2


Using a cursor:

Using a raw SQL query:

For both calls the result is the same:

Warning: theses procedures are made for quite small results. If you want to generate export a lot of data, please consider using another solution such as SQL*Plus. On my database, exporting a 1000 x ~300 characters result took 2 seconds.

The code

Leave a Reply