0

Is there a way using a DB2 database to systematically export all tables from a schema to separate csv files ? What I mean is to export all the tables in a schema without specifying table names. Maybe somehow getting all table names in a schema, then applying the export on those

mustaccio
28.7k24 gold badges60 silver badges77 bronze badges
asked Apr 24, 2018 at 11:50
2
  • Define "systematically". Is the export command not systematic enough? Commented Apr 24, 2018 at 12:44
  • @mustaccio So what i mean is to export all the tables in a schema without specifying table names. Maybe somehow getting all table names in a schema, then applying the export on those ?\ Commented Apr 24, 2018 at 12:47

5 Answers 5

2

You can easily create a small script that does this for you, or even do a loop on the command line. Assuming you are on a unix/linux platform something like below should do.

#> db2 connect to mydb
#> for t in $(db2 -x "select rtrim(tabschema) || '.' || rtrim(tabname) from syscat.tables where tabschema = 'DB2INST1'"); do db2 "export to $t.csv of del modified by coldel, select * from $t"; done

You can replace DB2INST1 with whatever schema you are interested in. If you want tables from several schematas you can use in:

#> for t in $(db2 -x "select rtrim(tabschema) || '.' || rtrim(tabname) from syscat.tables where tabschema in ('DB2INST1', ...)"); do db2 "export to $t.csv of del modified by coldel, select * from $t"; done

There are a lot of options that you can use to format the output from export, here I use modified by coldel but there is much more that you can configure. See:

https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.db2.luw.admin.cmd.doc/doc/r0008303.html

for more info.

answered Apr 24, 2018 at 15:15
1

With a bit of scripting you can certainly do something like this:

db2 -x list tables for schema your_schema | \
awk '{print "export to "1ドル".csv of del select * from "2ドル"."1ドル}' | \
db2 +p -x
answered Apr 24, 2018 at 16:55
0

If you want to do that via the SQL inteface, e.g. a JDBC client, you might employ the ADMIN_CMD() system stored procedure and run this compound statement:

begin 
 for r as c1 cursor with hold for 
 select tabschema, tabname from syscat.tables 
 where tabschema = 'YOUR_SCHEMA' 
 do 
 call admin_cmd(
 'export to /some/path/'||rtrim(r.tabname)||
 '.csv of del select * from '||rtrim(r.tabschema)||'.'||rtrim(tabname)); 
 end for; 
end

Note that the files will be created on the database server.

(Cursor declaraion requires with hold, because ADMIN_CMD() commits at the end of every invocation.)

answered Apr 24, 2018 at 17:07
0

Try this: https://www.sql-workbench.eu/ SQL Workbench/J is a free, DBMS-independent, cross-platform SQL query tool.

answered Jun 7, 2023 at 6:59
-1

Maybe try something like this:

db2move sample export -aw -l lobs -ts USERSPACE1
Laurenz Albe
62.1k4 gold badges57 silver badges93 bronze badges
answered Aug 16, 2019 at 8:44
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.