HOME

Syntax to connect to Oracle and DB2 from Unix

Contact Me

DB2 connection Syntax


Here is an example of how can you connect to DB2 database from Unix:

Here we are setting an instance named 'db2inst1', please initialize all remaining variables starting with $ sign.

export DB2INSTANCE=db2inst1
BIN="/db2/db2inst1/sqllib/bin"

#initialize all remaining variables starting with $ sign

${BIN}/db2 connect to $schema USER $user USING $pwd > /dev/null
${BIN}/db2 -x "insert into table1 (col1,col2,col3) values ('$val1' , '$val2' , '$val3' )"
${BIN}/db2 quit > /dev/null
		

Above code should successfully insert a row into table1.

Oracle connection syntax


Here is an example of how can you connect to Oracle database from Unix:

Oracle's executable named sqlplus is used for connecting to database.

We make use of Unix's here document. A here document is used to redirect input into an interactive shell script or program, which in this case is sqlplus.

We must set and export ORACLE_HOME, ORACLE_SID and PATH variables properly prior to an Oracle connection attempt.

Here, the connection string variable, CONNSTR, holds username, password and sid. I have shown a way to connect without revealing password here .

ORACLE_HOME=/u01/app/oracle/product/10g
export ORACLE_HOME
ORACLE_SID=sid1
export ORACLE_SID
PATH=$HOME:/usr/bin:/etc:/usr/sbin:/usr/ucb:$HOME/bin:/usr/bin/X11:/sbin:$ORACLE_HOME/bin:/usr/java5/bin
export PATH
CONNSTR="scott/pwd@sid1"

v_data=`sqlplus -s $CONNSTR << EOF 
set pages 0
set feed off
select 1 from dual ;
exit    
EOF`
		

Above code should successfully select number 1 into v_data variable.


Thanks...

TOP