I am trying to automate some things on DB2 and would like to use a batch file to do this. I create the following batch file test.bat:

db2cmd - -w "db2 -tvf test.db2"

I then try to do two commands in the test.db2 script, which work together in the CLP interactive mode:

!set db2instance=INST1;
!db2pd -db MYDB -hadr;

I then get the following output:

Database MYDB not activated on database partition 0. Option -hadr requires -db or -alldbs option and active database.

I'm using DB2 9.7 LUW on Windows 2008 R2.

  • 1
    Both commands in test.db are Windows commands, you don't need the CLP to run them. Try db2cmd -w "set db2instance=INST1 & db2pd -db MYDB -hadr" – mustaccio Aug 19 '15 at 19:08
  • @mustaccio This is great, but my script will obviously have a lot more commands to run than just these two. How exactly does the ampersand work as a new line? I don't understand why it won't work as escaped lines in my original script – ESP Aug 19 '15 at 19:29
  • 1
    Your original attempt did not work you invoked Windows commands from the DB2 CLP: the first one created a new environment and set the DB2INSTANCE variable in it, then quit, which destroyed that environment. The second command created yet another environment, in which DB2INSTANCE was not set (or set to a wrong value), so db2pd could not see the database. – mustaccio Aug 19 '15 at 19:37
  • @mustaccio You should post this as an answer, this explains what I was asking extremely well. – ESP Aug 19 '15 at 19:39
  • 1
    In NT's batch files command1 & command2 means "run command2 after command1 finishes, even if command1 fails". If you have lots of commands to chain together you can place them on separate lines; use ^ at the end of each line to escape the newline character and don't forget to eventually close the " (^ is the escape character in NT's batch files ... at least that's what I vaguely recall from my days administering Windows boxen!) – Dave Jones Aug 20 '15 at 11:33

I found a way to do this using two batch files:

db2cmd -c -i -w "test2.bat"

set db2instance=INST1
db2pd -db MYDB -hadr

This is effectively the same as running in interactive mode but if anybody has a way to do this using only one batch file please let me know!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.