fembot
fembot

Reputation: 87

Unable to escape single quotes db2 sql query run via shell script

There is probably a very simple solution to this, but I've tried every variation I could think of and still not able get rid of the error:

I'm running this via a shell script:

su - myid -c 'db2 -v "EXPORT TO '/tmp/file.out' OF DEL MODIFIED BY COLDEL| select A, B, C FROM mytable where C = '' and ( F = 1 or F = 2 or F = 3 or F = 4 ) "'

And this is the error I am getting:

SQL0104N  An unexpected token "=   and ( status" was found following "mytable
where C".  Expected tokens may include:  "<space>".  SQLSTATE=42601

Any help would be greatly appreciated

Upvotes: 0

Views: 1313

Answers (2)

fembot
fembot

Reputation: 87

I figured it out. Here's my updated query:

su - myid -c 'db2 -v "EXPORT TO '/tmp/file.out' OF DEL MODIFIED BY COLDEL| select A, B, C FROM mytable where C = '\'''\''  and ( F = 1 or F = 2 or F = 3 or F = 4 ) "'

I had to replace the single '' with '\'''\'' <---those are all single quotes

Upvotes: 0

chepner
chepner

Reputation: 532518

You could use an ANSI-quoted string if you are using bash:

su - myid -c $'db2 -v "EXPORT TO \'/tmp/file.out\'...'

in which you can quote a single quote using \'. In POSIX shells, you'll have to use double-quotes, and quote the embedded double-quotes:

su - myid -c "db2 -v \"EXPORT TO '/tmp/file.out' ..."

Upvotes: 1

Related Questions