ArtOfWarfare
ArtOfWarfare

Reputation: 21507

Running db2 from bash script not working?

I'm currently using bash on CentOS. DB2 is installed and db2 is on my path.

I have a few lines in a script which are supposed to update my db2 database, but they aren't working. As a minimal reproduction, I can do the exact same thing right in the bash command line and get the same error. Here's that reproduction:

$ db2 connect to PLT02345 user uni using uni; db2 update USM_USER set STATUS = 1 where NAME = 'asm_admin'

I expect this to set STATUS to 1 for everything in PLT02345.USM_USER where the NAME is currently asm_admin.

Instead, I get an error about "ASM_ADMIN" not being valid in the context where it's used. Here's the full output:

  Database Connection Information

Database server        = DB2/LINUXX8664 10.1.2
SQL authorization ID   = UNI
Local database alias   = PLT02345

DB21034E  The command was processed as an SQL statement because it was not a
valid Command Line Processor command.  During SQL processing it returned:
SQL0206N  "ASM_ADMIN" is not valid in the context where it is used.
SQLSTATE=42703

I'm confused - what about this makes it not valid? Is bash somehow mutilating the command and not passing everything as it should to db2?

Upvotes: 1

Views: 853

Answers (1)

Mr. Llama
Mr. Llama

Reputation: 20919

If you're running this from the command line, Bash will drop the 's off 'asm_admin' because it simply assumes you're passing a string. The end result is the SQL becoming WHERE name = asm_admin which is invalid.

To correct this, you need to quote your whole command:

db2 "update USM_USER set STATUS = 1 where NAME = 'asm_admin'"

Upvotes: 2

Related Questions