0

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

6
  • 3
    Have you tried to work calling the Db2 Command Line Processor directly instead, not as a Java application? Like db2 "connect ...", db2 -tvf part3.sql? Commented Nov 7, 2021 at 17:04
  • Or if you run your application under z/OS UNIX System Services (Db2 Command Line Processor is a Java application there, and there is no clp.jar file on Linux, Unix and Windows platforms), then remove the db2-luw tag. Commented Nov 8, 2021 at 8:00
  • @MarkBarinstein from the db2 CLP i am able to successfully call the commands you stated, but I was wondering if you can execute them from a sh script
    – Fnechz
    Commented Nov 8, 2021 at 22:33
  • What's the OS where you run this script? Commented Nov 9, 2021 at 7:04
  • 1
    I don't have such a system at hand, but according to the documentation you can't pass distinct commands to Db2 CLP as you showed. Refer to the Running the Db2 command line processor in batch mode topic. You must run a complete script of commands in a batch mode (from sh script as well, of course) starting from CONNECT, if you want to run some SQL statements inside. You may run whatever number of such scripts in your sh file. Every such an invocation uses its own connection to the database. Commented Nov 10, 2021 at 9:24

1 Answer 1

1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.