Skip to Main Content
  • Questions
  • Generate a CSV Blob and store it in a table

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Lovlesh.

Asked: March 10, 2017 - 6:49 am UTC

Last updated: March 11, 2017 - 1:52 am UTC

Version: PLSQL

Viewed 10K+ times! This question is

You Asked

Hi Tom,

i have a requirement that i need to export a table contents into a csv file and store it as a blob in other table. But i dont have the provision to store the file in a directory so i need to perform all actions together. How can i do that?

and Connor said...

Something like this perhaps ?

SQL> create table t ( c clob );

Table created.

SQL> declare
  2    l_clob  clob;
  3    l_row   varchar2(4000);
  4  begin
  5    dbms_lob.createtemporary(l_clob, true);
  6    for i in ( select * from user_tables )
  7    loop
  8      l_row := i.table_name||','||i.num_rows||','||i.blocks||chr(10);
  9      dbms_lob.writeappend (l_clob, length(l_row), l_row);
 10    end loop;
 11
 12    insert into t (c) values ( l_clob );
 13
 14    dbms_lob.freetemporary(l_clob);
 15  end;
 16  /

PL/SQL procedure successfully completed.

SQL>
SQL> select * from t;

C
---------------------------------------------------------------------------
JOB_PARAMS,1,5
JOB_LOG,2,5
PRODUCT,1,5
U_CSS_COLORFAM,4,5
RESULTS,500,5
EMP1,14,


Rating

  (1 rating)

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

Comments

Lovlesh, March 10, 2017 - 8:51 am UTC

Hi Connor,

Thanks a lot for the response. But what i am looking for is that the blob content should be of extension .csv as i need to access the file later.
Connor McDonald
March 11, 2017 - 1:52 am UTC

A clob (or blob) is not a file...it is the *contents* of the file.

When you *extract* the clob/blob can you store it (or present it, eg, over http) however you like (as a csv file, or a raw stream, etc).

We store the *content* - you decide how to then *present* it to the calling environment

More to Explore

CLOB/BLOB/etc

Complete documentation on Securefiles and Large Objects here