# Run DB2 export command in loop

**URL:** <https://community.unix.com/t/run-db2-export-command-in-loop/270862>\
**Category:** Shell Programming and Scripting\
**Created:** [August 4, 2010, 5:40pm UTC](https://community.unix.com/t/run-db2-export-command-in-loop/270862 "2010-08-04T17:40:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![srimitta](https://community.unix.com/letter_avatar/srimitta/32/6_449c533f586939b6e5f246632a46421f.png) [@srimitta](https://community.unix.com/u/srimitta)\
**Post date:** [August 4, 2010, 5:40pm UTC](https://community.unix.com/t/run-db2-export-command-in-loop/270862/1 "2010-08-04T17:40:45Z")

</div>

Hi All,

I have list of 100 table names in a file, how to read table name from and pass to DB2 export command and run for all tables in loop.

Please help me with script.

```nohighlight
db2 EXPORT TO ~/data_export/<table name from file>.ixf OF IXF MESSAGES messages.txt "SELECT * FROM ITG.<Table Name from file>"

```

Thanks  
Srimitta

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [August 4, 2010, 5:50pm UTC](https://community.unix.com/t/run-db2-export-command-in-loop/270862/2 "2010-08-04T17:50:31Z")

</div>

```nohighlight
$ cat file_with_table_names
table1
table2
table3

$ xargs -I{} echo db2 EXPORT TO \~/data_export/{}.ixf OF IXF MESSAGES messages.txt \"SELECT \* FROM ITG.{}\" < file_with_table_names | sh

```

---

<div class="post-metadata">

**Author:** ![srimitta](https://community.unix.com/letter_avatar/srimitta/32/6_449c533f586939b6e5f246632a46421f.png) [@srimitta](https://community.unix.com/u/srimitta)\
**Post date:** [August 4, 2010, 6:44pm UTC](https://community.unix.com/t/run-db2-export-command-in-loop/270862/3 "2010-08-04T18:44:20Z")

</div>

Hi Scott,

Thanks for the reply.

I ran script and getting below error message, any idea.

```nohighlight
$ ksh -x export_db2_tables.ksh
+ cd /home/db2inst2/data_export/scripts/
+ cat table_names
table1
table2
table3
+ db2 connect to ITG

   Database Connection Information

 Database server = DB2/AIX64 9.5.2
 SQL authorization ID = DB2INST1
 Local database alias = ITG

+ xargs -I{} db2 EXPORT TO ~/data_export/{}.ixf OF IXF MESSAGES messages.txt "SELECT * FROM ITG.{}"
+ 0< table_names
+ sh
sh: SQL0104N: not found.
sh[2]: found: not found.
sh[4]: SQL0104N: not found.
sh[5]: found: not found.
sh[7]: SQL0104N: not found.
sh[8]: was: not found.

```

Thanks  
Srimitta

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [August 4, 2010, 6:55pm UTC](https://community.unix.com/t/run-db2-export-command-in-loop/270862/4 "2010-08-04T18:55:32Z")

</div>

Hi.

Sorry about that 🙂

```nohighlight
while read TABLE; do
  db2 EXPORT TO ~/data_export/$TABLE.ixf OF IXF MESSAGES messages.txt "SELECT * FROM ITG.$TABLE"
done < file_with_table_names 

```

My globbing brain is not engaged! If it doesn't work, try changing \* to \*

---

<div class="post-metadata">

**Author:** ![srimitta](https://community.unix.com/letter_avatar/srimitta/32/6_449c533f586939b6e5f246632a46421f.png) [@srimitta](https://community.unix.com/u/srimitta)\
**Post date:** [August 5, 2010, 11:30am UTC](https://community.unix.com/t/run-db2-export-command-in-loop/270862/5 "2010-08-05T11:30:19Z")

</div>

Thanks Scott, Excellent it's working.

Srimitta
