Skip to Main Content
  • Questions
  • Unable to select all fetch all columns using sqlplus

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Prashant.

Asked: November 27, 2016 - 6:35 pm UTC

Last updated: November 29, 2016 - 1:10 am UTC

Version: 11.2.0.2.0

Viewed 10K+ times! This question is

You Asked

Hi Tom,
I am trying to spool the columns of a table(having more than 200 columns), but i am not able to export all columns, there are only limited number of columns returned from sqlplus. Sharing my piece of code from script:

SET COLSEP ',' LINESIZE 32767 NUMWIDTH 18 LONG 2000 ECHO OFF
SET TRIMOUT ON ARRAYSIZE 5000 AUTOCOMMIT OFF FLUSH OFF HEADING ON TERMOUT OFF
SET PAUSE OFF SERVEROUTPUT ON TRIMSPOOL ON VERIFY OFF
SET UNDERLINE OFF PAGESIZE 0 ESCAPE OFF HEADING OFF FEEDBACK OFF WRAP OFF

SPOOL &spooldrive.&spooldir.TABLE_XYZ.csv

SELECT /*csv*/
' Fetching data from table TABLE_XYZ',SYSTIMESTAMP FROM dual;
SELECT /*csv*/
xmlagg(xmlelement(e,column_name||',').extract('//text()') order by column_id)
from USER_TAB_COLS WHERE TABLE_NAME='TABLE_XYZ';
SELECT /*csv*/
* from TABLE_XYZ where project_id = &p_id;
SPOOL OFF;

Is there anything that i am missing? or is there any way that i can print all columns no matter it exceeds the line buffer size?

and Connor said...

If the output is more than 32767, then sqlplus wont display it unless you turn wrap on (and then the line will wrap).

SQL> create table TABLE_XYZ ( col1 char(200) );

Table created.

SQL>
SQL> declare
  2    ins_sql varchar2(32000) := q'{insert into TABLE_XYZ values ('x'}';
  3  begin
  4  for i in 2 .. 200 loop
  5    execute immediate 'alter table TABLE_XYZ add col'||i||' char(200)';
  6    ins_sql := ins_sql || ',''x''';
  7  end loop;
  8  execute immediate ins_sql||')';
  9  end;
 10  /

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> SET COLSEP ',' LINESIZE 32767 NUMWIDTH 18 LONG 2000 ECHO OFF
SQL> SET TRIMOUT ON ARRAYSIZE 5000 AUTOCOMMIT OFF FLUSH OFF HEADING ON TERMOUT OFF
SQL> SET PAUSE OFF SERVEROUTPUT ON TRIMSPOOL ON VERIFY OFF
SQL> SET UNDERLINE OFF PAGESIZE 0 ESCAPE OFF HEADING OFF FEEDBACK OFF WRAP OFF
SQL>
SQL> SELECT * from TABLE_XYZ;
rows will be truncated

rows will be truncated

rows will be truncated


...
...

SQL> set wrap on
SQL> SELECT * from TABLE_XYZ;
x                                                                                                                                                                                               ,x                                                                                                                                                                                               ,x
                                                                                  ,x                                                                                                                                                                                               ,x                                                                                                                                                                                               ,x
                                                                                                                                                             ,x                                                                                                                                                                                               ,x
                                               ,x                                                                                                                                                                                               ,x                                                                                                                                                                                               ,x
                                                                                                                          ,x                                                                                                                                                                                               ,x
            ,x                                                                                                                                                                                               ,x                                                                                                                                                                                               ,x
                                                                                               ,x                                                                                                                                                                                               ,x                                                                                                                                                                                               ,x
                                                                                                                                                                          ,x                                                                                                                                                                                               ,x
                                                            ,x                                                                                                                                                                                               ,x                                                                                                                                                                                               ...


Rating

  (1 rating)

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

Comments

Re

Prashant Patel, November 28, 2016 - 10:36 am UTC

Thanks for the update. I want to export the columns in csv so i want to have it in single line no matter how many columns it has. How can i achieve this?
Connor McDonald
November 29, 2016 - 1:10 am UTC

You would probably need to do it yourself, for example, using UTL_FILE, but even that has line size limitations.

After that, you're probably need to do something yourself in C or Java or similar 3GL.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library