If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > executing multiple .sql files

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-26-07, 21:47
teckguan teckguan is offline
Registered User
 
Join Date: Dec 2007
Posts: 2
executing multiple .sql files

Hi everyone,

I am still new to both Shell script and db2. Currently I have a lot of .sql files to be executed. Therefore, I would like to ask how am I going to execute the files by just a few lines of command in Shell script? Thanks.

Regards,
Teck Guan.
Reply With Quote
  #2 (permalink)  
Old 12-26-07, 21:59
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Create a shell script as follows (I will call it db2script.sh):

db2 -tvf file1.sql > file1.out
db2 -tvf file2.sql > file2.out
db2 -tvf file3.sql > file3.out
db2 -tvf file4.sql > file4.out
db2 -tvf file5.sql > file5.out

Make sure each of the above files has a "connect to db-name;" as the first statement. Each SQL statement will be terminated by a ";".

Then issue the following command to give execute authority on the script:
chmod 775 db2script.sh

The execute the script (this assumes you are on the correct path):
./db2script.sh
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 12-26-07, 22:24
teckguan teckguan is offline
Registered User
 
Join Date: Dec 2007
Posts: 2
Thanks. However, if I have 500 .sql files, I will have to type 500 lines. Therefore, I would like to ask is there any ways to execute the .sql files in easier way?

Regards,
Teck Guan.
Reply With Quote
  #4 (permalink)  
Old 12-27-07, 02:35
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You could use a simple loop in the shell script:
Code:
db2 "connect to <dbname>"
for f in *.sql; do
    db2 -f $f > $f.output
done
Note that Marcus used some additional options for the db2 CLP - namely, -t and -v. You may want to read up on those options (and others) and determine which ones you need.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On