1

I want to delete certain rows from a table based on my input values to a unix shell script. I am making connections and terminating it inside my shell script.

If you notice right now the field is hardcoded in the where clause.

How can I pass that value as a parameter to the script?

#!/bin/ksh
set -x
export DB2INSTANCE=db2clnt1
db2 ""CONNECT TO DBNAME user USER using passwd""
db2 ""delete from table where field =\'abcdefghijklmn\'""
db2 ""CONNECT RESET"" 
WEBjuju
  • 496
  • 2
  • 4
  • 13

1 Answers1

1

Referencing this answer from another community...

The DB2 CLP does not support using parameter markers.

However, when you are running db2 non-interactively (i.e., from the shell), you can substitute shell variables:

That should leave you with something like:

#!/bin/ksh
set -x
export DB2INSTANCE=db2clnt1
db2 "CONNECT TO DBNAME user USER using passwd"
db2 "delete from table where field = '${1}'"
db2 "CONNECT RESET"

which can be called with the field value on the command line

./myksh.sh "delete-this-field"

It is important to remember though that...

...this is just using shell substitution you'll need to handle adding single quotes around your strings...[and] if your strings have single quotes this becomes more complicated.

WEBjuju
  • 496
  • 2
  • 4
  • 13