Fnechz
Fnechz

Reputation: 161

Executing db2 commands from bash script giving errors?

I am a noob in running db2 commands in the unix environment, so I have been trying to connect to a db2 instance from a bash script. However I get errors here is what my script looks like:

#!/bin/bash

DB2="java com.ibm.db2.clp.db2"
$DB2 "connect to <db2 server here> user **** using ****"

I get a DSNC102I : The option "connect to <db2 server> user **** using ****" specified after the "db2" command is incorrect.

I do not know what to do from here. Currenty I am able to run an sql script from the same bash script by using $DB2 -tvf part3.sql where both connection details and sql queries are in the part3.sql file. Why can't I achieve the same results by writing the sql commands themselves in the bash script.

PS: I want this since I my bash script is required to accept any db2 instance/ schema to conduct queries as a parameter to the bash script

Upvotes: 0

Views: 1841

Answers (1)

jcockayne
jcockayne

Reputation: 26

It looks like you're using the CLP under USS and it is interpreting the connect statement as an option flag instead of a command.

Putting the connect statement and the statements to run in a file at run time should do what you need - this script takes parameters for db2 server, username, and password so you can remove them from part3.sql:

#!/bin/bash
DB2="java com.ibm.db2.clp.db2"
echo "connect to $1 user $2 using $3;" > temp.sql
cat part3.sql >> temp.sql
$DB2 -tvf temp.sql
rm temp.sql

The connect statement is put into a temp file, then the contents of the part3.sql file are copied in and the file is run by the CLP. Finally the temp file is removed.

Upvotes: 1

Related Questions