Informix: How to get table contents and column names using dbaccess?

Suppose I have:

  • Informix database named "my_database"
  • a table named "my_table" with columns "col_1", "col_2" and "col_3":

I can extract the contents of a table by creating a my_table.sql script like:

unload to "my_table.txt"
select * from my_table;

      

and by calling dbaccess from the command line:

dbaccess my_database my_table.sql

      

This will create a file my_table.txt with content like:

value_a1|value_a2|value_a3
value_b1|value_b2|value_b3

      

Now, what do I need to do if I want to get the column names in my_table.txt ? How:

col_1|col_2|col_3
value_a1|value_a2|value_a3
value_b1|value_b2|value_b3

      

+2


a source to share


3 answers


None of the standard Informix tools put column names at the top of the output the way you want.

SQLCMD (not Microsoft newbie - the original one available from the IIUG Software Archive ) has the ability to do this; use parameter -H

for column headings (and -T

for getting column types).



sqlcmd -U -d my_database -t my_table -HT -o my_table.txt
sqlunload -d my_database -t my_table -HT -o my_table.txt

      

SQLCMD can also do CSV output if you need it (but - a bug - it does not format the column rows or column rows correctly).

+1


a source


Found an easier solution. Put the headers in one file, say header.txt

(it will contain one line " col_1|col_2|col_3

") to merge the header file and your output file:



cat header.txt my_table.txt > my_table_wth_head.txt

      

0


a source


Why aren't you using dbschema?

Get the schema of a single table (show the entire database without the -t option)

dbschema -d [DBName] -t [DBTable] > file.sql

      

To get the schema of a single stored procedure

dbschema -d [DBName] -f [SPName] > file.sql

      

0


a source







All Articles