Skip to Main Content
  • Questions
  • How to read table, create csv, zip it and store in another table

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Giordano.

Asked: August 22, 2017 - 10:08 am UTC

Last updated: August 22, 2017 - 12:52 pm UTC

Version: Oracle 12.6.0.53

Viewed 10K+ times! This question is

You Asked

Hello,

my problem is I need to read all records from one table, create a csv with the records (not csv file I have not access to filesystem, but maybe with temp table), zip the csv and store it in another oracle table.

Is it possible with a procedure PLSQL?

Thank for help, I hope my english is good enough.

Giordano

and Chris said...

Why on earth do you want to store a zipped CSV of one table in another in the database?!

It's possible using PL/SQL, doing something like:

- Write the output to the server using UTL_FILE
- Use the scheduler to run a script that will zip this file
- Read the file back into a blob column using utl_file

...but this is a rather convoluted process.

What's your real requirement here? Store a copy of a table so you can recover the data if there's an issue? And compress it to save space?

If so, you could rely on your normal database backups.

Or, if you need the copy to be "online", load the data a copy of the current table with compression enabled. You can read more about this at:

https://oracle-base.com/articles/11g/table-compression-enhancements-11gr1

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Reply your question

Giordano Babolin, August 22, 2017 - 10:25 am UTC

Thanks for your answer.

My problem is I don't have a permission to write a file so I need to this by a temporary table.

My goal is to create a zip file to future user download directly from application
Chris Saxon
August 22, 2017 - 10:47 am UTC

If you want to allow people to download your data, offer this functionality in your UI code.

Creating it using PL/SQL and storing in a database table is a crazy way to do this!

Possible, but maybe not really the best way?

Kim Berg Hansen, August 22, 2017 - 11:00 am UTC

@Giordano,

I'm in general an advocate of doing as much data work in the database as possible.

Creating a zip file of the data for download, however, is something I personally would do on the web-server rather than the database. Either on-the-fly when the download is requested, or (if that is too slow) have the web-server periodically read the data and create the zip-file as a kind of "cache".

But if for example it's an APEX or mod_plsql application with a web-server that's basically just a proxy without any functionality or file services, then it could possibly be needed to do it in the database. If so, then it should be possible along these lines:

1) Build the CSV manually in a CLOB.
2) DBMS_LOB.CONVERTTOBLOB to get a BLOB.
3) UTL_COMPRESS.LZ_COMPRESS to zip the BLOB into another BLOB.
4) Store the resultant BLOB in a table.

But I suggest only to try it if really necessary. The most logical place for this functionality (in my opinion) is the web-server.

Cheerio
/Kim

Chris Saxon
August 22, 2017 - 12:52 pm UTC

Indeed. Zipping results for download isn't really something you should do in the DB.

More to Explore

Design

New to good database design? Check out Chris Saxon's full fundamentals class.