473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Export data as well as the corresponding column names?

mjf
Hello,
Is there a way to export the data from some tables, along with their
corresponding column names? I know I can use "export" command to export
the data from a table to a DEL file (the users need to be able to read
the file directly, which leaves the choice of a DEL file), but only the
data will be in the file. The users would like to see the column names
side by side with the data as well. I wonder if there is an easy way to
do so without too much coding.
Thanks.

Feb 3 '06 #1
13 27385
Use ixf format will have table DDL, include the column name. But not
readable directly.
The easy way is like this
1. db2 export to tablename.del
2. db2 select from tablename > column.del
3. copy column.del + tablename.del

mjf wrote:
Hello,
Is there a way to export the data from some tables, along with their
corresponding column names? I know I can use "export" command to export
the data from a table to a DEL file (the users need to be able to read
the file directly, which leaves the choice of a DEL file), but only the
data will be in the file. The users would like to see the column names
side by side with the data as well. I wonder if there is an easy way to
do so without too much coding.
Thanks.


Feb 3 '06 #2
You may be able to use a UNION query that SELECTs the COLUMN names on
top.

SELECT 'Column1', 'Column 2', Column 3'....
UNION ALL
SELECT ......

B.

Feb 3 '06 #3
mjf
Hello Brian, the method you suggested is very promising. Thanks a lot!
I did it to a test table that has the following DDL:

db2 "describe table test"

Column Type Type
name schema name Length
Scale Nulls
------------------------------ --------- ------------------ --------
----- ------
COL1 SYSIBM SMALLINT 2
0 Yes
COL2 SYSIBM VARCHAR 5
0 Yes
COL3 SYSIBM VARCHAR 5
0 Yes

db2 "export to test.del of del modified by chardel'' coldel; decpt,
select col1, 'col1 ', col2, 'col2 ', col3, 'col3 ' from test"

The export was successful, and here is the content in test.del:
1;'col1 ';'test1';'col2 ';'val1';'col3 '
2;'col1 ';'test2';'col2 ';'val2';'col3 '
3;'col1 ';'test3';'col2 ';'val3';'col3 '

I added the blanks in the column names so the users can spot each pair
of value/column-name easily. When there are lots of columns in the
table, the above would wrap around to next lines and made it hard to
read, though. Do you know any way of formatting it like the following?

1;'col1 ';
'test1';'col2 ';
'val1';'col3 '

2;'col1 ';
'test2';'col2 ';
'val2';'col3 '

3;'col1 ';
'test3';
'col2 ';
'val3';'col3 '
I need to set up a daily cron job to do such a file, so I need to do
everything automatically. I guess I can always write a PERL script to
separate the pairs, but wonder if there is yet another easy way to do
it.
Thanks again.

Feb 3 '06 #4
I don't think so. It's a bit beyond my knowledge of db2.

If you are using a cron job, perl seems easy enough, or any of those
nifty tools that filter columns in order.

B.

Feb 6 '06 #5
mjf wrote:
Hello Brian, the method you suggested is very promising. Thanks a lot!
I did it to a test table that has the following DDL:

db2 "describe table test"

Column Type Type
name schema name Length
Scale Nulls
------------------------------ --------- ------------------ --------
----- ------
COL1 SYSIBM SMALLINT 2
0 Yes
COL2 SYSIBM VARCHAR 5
0 Yes
COL3 SYSIBM VARCHAR 5
0 Yes

db2 "export to test.del of del modified by chardel'' coldel; decpt,
select col1, 'col1 ', col2, 'col2 ', col3, 'col3 ' from test"

The export was successful, and here is the content in test.del:
1;'col1 ';'test1';'col2 ';'val1';'col3 '
2;'col1 ';'test2';'col2 ';'val2';'col3 '
3;'col1 ';'test3';'col2 ';'val3';'col3 '

I added the blanks in the column names so the users can spot each pair
of value/column-name easily. When there are lots of columns in the
table, the above would wrap around to next lines and made it hard to
read, though. Do you know any way of formatting it like the following?

1;'col1 ';
'test1';'col2 ';
'val1';'col3 '

2;'col1 ';
'test2';'col2 ';
'val2';'col3 '

3;'col1 ';
'test3';
'col2 ';
'val3';'col3 '


You could simply concatenate all values into a single, long string and then
use some logic to insert the line breaks. A dedicated UDF comes to mind.
I would probably code it in Java or C/C++ as it is a bit easier to iterate
over strings that way.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Feb 6 '06 #6
If he's going to use a UDF, wouldn't a FOR loop do it?

Someting like:

FOR Loop AS SELECT col1, col2, ...
Long_String = Long_String || Loop.Col1 || Loop.Col2...
END

Or would that take too long?

B.

Feb 6 '06 #7
In article <11**********************@o13g2000cwo.googlegroups .com>,
mjf (mi****@excite.com) says...
db2 "export to test.del of del modified by chardel'' coldel; decpt,
select col1, 'col1 ', col2, 'col2 ', col3, 'col3 ' from test"


On windows I would add a column with x'0D0A' to get what you want, on
Unix X'0A' should work.

This should work (not tested):

db2 "export to test.del of del
modified by chardel'' coldel; decpt,
select col1, 'col1 ', x'0A',
col2, 'col2 ', x'0A',
col3, 'col3 '
from test"
Feb 6 '06 #8
It's almost there!
I ran the following statement:

db2 "export to test.del of del modified by chardel'' coldel; decpt,
select 'col1', col1, x'0A', 'col2', col2, x'0A', 'col3', col3 from
test"

And the result is:

'col1';1;'
';'col2';'test1';'
';'col3';'val1'
'col1';2;'
';'col2';'test2';'
';'col3';'val2'
'col1';3;'
';'col2';'test3';'
';'col3';'val3'

I guess that's the best we can do without using Java or C/C++?
Thank all of you very much for your help!

Feb 7 '06 #9
mjf
I posted the above reply but didn't realize someone else has logged on
to gmail on the same machine and the email and nickname of the message
became his.
By the way, does anyone know how to delete a posted message? I'd like
to delete the above message and re-post it using my own id.
Thanks.
mjf

Feb 7 '06 #10
mjf wrote:
I posted the above reply but didn't realize someone else has logged on
to gmail on the same machine and the email and nickname of the message
became his.
By the way, does anyone know how to delete a posted message? I'd like
to delete the above message and re-post it using my own id.
Thanks.
mjf

In Thunderbird, right click mouse and pick Cancel Message.
--
Serge Rielau
DB2 Solutions Development
DB2 UDB for Linux, Unix, Windows
IBM Toronto Lab
Feb 7 '06 #11
mjf
Serge, thanks!
I removed the message I posted yesterday with someone else's id
(accidentally), and am re-posting it here with my own id:
~~~~~~~
t's almost there!
I ran the following statement:

db2 "export to test.del of del modified by chardel'' coldel; decpt,
select 'col1', col1, x'0A', 'col2', col2, x'0A', 'col3', col3 from
test"

And the result is:

'col1';1;'
';'col2';'test1';'
';'col3';'val1'
'col1';2;'
';'col2';'test2';'
';'col3';'val2'
'col1';3;'
';'col2';'test3';'
';'col3';'val3'

I guess that's the best we can do without using Java or C/C++?
Thank all of you very much for your help!
~~~~~~~~~~~~~

Feb 8 '06 #12
mjf wrote:
Serge, thanks!
I removed the message I posted yesterday with someone else's id
(accidentally), and am re-posting it here with my own id:
~~~~~~~
t's almost there!
I ran the following statement:

db2 "export to test.del of del modified by chardel'' coldel; decpt,
select 'col1', col1, x'0A', 'col2', col2, x'0A', 'col3', col3 from
test"


How about this:

SELECT 'col1;' || RTRIM(CHAR(col1)) || ';' || x'0A' || 'col2;''' || col2 ||
''';' || x'0A' || 'col3;''' || col3 || '''' FROM test

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Feb 8 '06 #13
mjf
Knut Stolze wrote:
How about this:

SELECT 'col1;' || RTRIM(CHAR(col1)) || ';' || x'0A' || 'col2;''' || col2 ||
''';' || x'0A' || 'col3;''' || col3 || '''' FROM test

--
Knut Stolze
DB2 Information Integration Development
IBM Germany


Wow! That's great! It worked perfectly! Here is the result after
running your statement:
1
---------------------------------------
col1;1;
col2;'test1';
col3;'val1'
col1;2;
col2;'test2';
col3;'val2'
col1;3;
col2;'test3';
col3;'val3'

3 record(s) selected.

Thank you very much, Knut!

Feb 8 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joe Gazda | last post by:
I'm a relative newbie to PHP, but have been able to put together some PHP code to generate a CSV/XLS file from a Query result. Now, I would like to include custom column names instead of the MySQL...
2
by: Bart Van der Donck | last post by:
Hello, I am using MySQL 4.0. Say that I have a table named "mytable" having a column "ID" and a column "test columnname". When dumping: mysqldump --opt DATABASE -uUSER -hHOST -pPASS >...
1
by: noor | last post by:
Hi I have been working since 2 days to device a method to export sql table into csv format. I have tried using bcp with format option to keep the column names but I'm unable to transfer the file...
2
by: John T. McCraw | last post by:
How can I retrieve just the column names from an Access table. I don't need the data, just the column names.
2
by: Joe Griffith | last post by:
I'm using a Win Forms Data Grid View control in unbound mode. When I add columns using the wizard the first item is the column name. Everything works fine. However, if you return to the...
5
by: seddy | last post by:
Hello ! I`m kinda new to it so I found this `job` very hard therefor I ask for Your help. So, the thing is... I have this XML file ( http://www.izishop.net/export.php ) which I need to open...
2
by: Designing Solutions WD | last post by:
Hello, I have some questions on my options available. I have to export some tables to csv files to enable another department to process the files. What I need is a way to do this in ms sql...
1
by: sylviasepeng | last post by:
Hi, I am beginner in Oracle and have the following problem. am working with reports and forms and now have to add a column and when i check the select statement to see if the the column has been...
1
by: christianlott1 | last post by:
I want to provide users with an interface to create a custom merge (all in Access, not Word). User will put in a set of brackets ("<>") in a memo field and when they click the merge button it will...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.