How to execute DB2 command through UNIX command line

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
Amarpreet
Participant
Posts: 20
Joined: Tue Apr 04, 2006 11:45 pm
Location: India
Contact:

How to execute DB2 command through UNIX command line

Post by Amarpreet »

I want to execute DB2 command using TELNET session. What is the directory path to get to the DB2 prompt. Then how can I connect to my database in particular instance.
I want to use executecommand stage in my sequence to execute one DB2 command. what should I write in the stage(starting from connect to db2 till execute the command)?
vmcburney
Participant
Posts: 3593
Joined: Thu Jan 23, 2003 5:25 pm
Location: Australia, Melbourne
Contact:

Post by vmcburney »

To run DB2 commands you need some DB2 environment settings such as DB2INSTANCE and the DB2 path. Your user id may already have these, try typing DB2 to see what happens. Your DB2 admin should be able to add those variables to your .profile file in your home directory. You also might be able to set them by running the dsenv file in your DataStage $DSHOME directory (if the DB2 settings are in there).

Basically the DB2INSTANCE points you at the path of the DB2 client and lets you run it from the command line much like SQLPLUS. Have a look at the DB2 documentation online at www.ibm.com for the command syntax.
bcarlson
Premium Member
Premium Member
Posts: 772
Joined: Fri Oct 01, 2004 3:06 pm
Location: Minnesota

Post by bcarlson »

Talk to your DBA. If you know the DB2 instance and database name, then you can pretty much get started.

For example, assume your instance is called "db2inst1" and the database name is "mydb2db". There are 2 main variables that you need to have set in order to run DB2 commands - DB2INSTANCE and DB2DBDFT.

You can create the variable called DB2INSTANCE="db2inst1" and DB2DBDFT="mydb2db". For DB2, the instance is actually a Unix user as well, so it has its own home directory. You can reference it as ~${DB2INSTANCE}. Within the instance directory, there is a profile that can be run to setup paths. At the Unix command line, do the following:

Code: Select all

export DB2INSTANCE=db2inst1
export DB2DBDFT="mydb2db"
. ~${DB2INSTANCE}/sqllib/db2profile
Once these are run, you should be able to simply type 'db2' at the commandline and go into interactive mode. Otherwise, you can run queries like this:

Code: Select all

db2 "select count(*) from myschema.mytable"
or long queries like this:

Code: Select all

db2 -stv +p <<- EOF
    select a.acct_id, b.customer_id
    from myschema.acct a,
        myschema.cust_x_acct x,
        myschema.cust b
    where x.acct_id = a.acct_id
    and b.cust_id = x.cust_id
    ;
EOF
Hope this helps.

Brad.
Post Reply