Skip to Main Content
  • Questions
  • Exporting multiple rows from a table to single row text using utl

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Pradeep.

Asked: November 15, 2017 - 4:53 pm UTC

Last updated: November 29, 2017 - 2:32 pm UTC

Version: Oracle 11g

Viewed 1000+ times

You Asked

In my project, I need export multiple rows from a single column to single row text using utl file
Example below
My table : Employees
Employee_name
-----------------------------
Smith
John
Tom
Adam

And my output text file should export like below

SmithJohnTomAdam

There should not any space are separator between the rows. Even a million rows should export into
Single row in text format

and Chris said...

Well, if you know the final string will be less than 4,000 bytes long* you can use listagg in SQL:

with rws as (
  select rownum r, lpad(chr(rownum+64), 3, chr(rownum+64)) str 
  from   dual
  connect by level <= 5
)
  select listagg(str, '') within group (order by r) s from rws;

S                 
AAABBBCCCDDDEEE


If it'll be longer than that, but < 32,767 you can build up the string in PL/SQL:

declare
  s varchar2(32767);
begin
  
  for rws in (
    select rownum r, lpad(chr(rownum+64), 3, chr(rownum+64)) str 
    from   dual
    connect by level <= 10000
  ) loop
    s := s || rws.str;
  end loop;

  dbms_output.put_line ('Length: ' || length(s));
  dbms_output.put_line ('Start: ' || substr(s, 1, 30));
end;
/

Length: 30000
Start: AAABBBCCCDDDEEEFFFGGGHHHIIIJJJ


And if you're longer than 32,767 bytes, you can use a clob:

declare
  s clob;
begin
  
  dbms_lob.createtemporary ( s , true );
  
  for rws in (
    select rownum r, lpad(chr(rownum+64), 3, chr(rownum+64)) str 
    from   dual
    connect by level <= 20000
  ) loop
    dbms_lob.append ( s, rws.str );
  end loop;

  dbms_output.put_line ('Length: ' || dbms_lob.getlength(s));
  dbms_output.put_line ('Start: ' || dbms_lob.substr(s, 30, 1));
end;
/

Length: 60000
Start: AAABBBCCCDDDEEEFFFGGGHHHIIIJJJ


* If you're on 12.1 and up you can go up to 32,767 with listagg if you have MAX_STRING_SIZE set to EXTENDED.

Rating

  (3 ratings)

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

Comments

Pradeep M, November 16, 2017 - 12:13 pm UTC

But I have to export as a text using UTL FILE .
HOW TO DO IN UTL
Chris Saxon
November 16, 2017 - 1:49 pm UTC

YOU TAKE THE OUTPUT OF THESE AND WRITE THEM TO THE FILE!

You can find examples using UTL_FILE elsewhere on this site...

Though if you want to write more than 32k to one line, you need write the file in binary.

dbms_xslprocessor.clob2file

Robert, November 16, 2017 - 2:02 pm UTC

You may not need to write the file in binary if a line is over 32K. You can use dbms_xslprocessor.clob2file.
Chris Saxon
November 16, 2017 - 4:51 pm UTC

Not familiar with that one, thanks for sharing.

Need to write 600000000 length of character in a single line

Pradeep M, November 28, 2017 - 5:05 pm UTC

How to write to binary file. But I need a output as text file. It's a single line data with 600000000 character length concatenated using xmlagg n XML cast.Also DBMS_xslprocessor.clob2file package not found in my database. How to use that one. Also explain how write in binary file n save as text format in output
Chris Saxon
November 29, 2017 - 2:32 pm UTC

Just supply "b" when opening the file. If you do:

declare
  fhandle  utl_file.file_type;
begin
  fhandle := utl_file.fopen(location     => 'TMP',
                           filename     => 'test.txt',
                           open_mode    => 'wb');
  utl_file.put_raw(fhandle, utl_raw.cast_to_raw('test' || chr(10)), true);
  utl_file.fclose(fhandle);

end;
/


Then you'll get the file test.txt containing the text "test":

bash-4.1$ cat test.txt
test

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.