sql statement in a here document - 2nd try

Hello all

I have posted this question before but no one seemed to know what the
problem was or how to solve it, so I'm giving it another try ..

I'm having some problems with an sql statement in a here document.
The following code works fine:
#!/bin/ksh
..
..
result=$(sqlplus -s myuser/mypass [at] myinstance << EOF
select tname from tab
/
EOF)

echo $result | tr " " "\n"


but when I add a "where field like 'XXX' " statement with single
quotes, such as:
#!/bin/ksh
..
..
result=$(sqlplus -s myuser/mypass [at] myinstance << EOF
select tname from tab where tname like 'VOUCHER'
/
EOF)

echo $result | tr " " "\n"

I get the the content of the current directory and an ORACLE error
message, for example:
..
..
sqlnet.log
testing
users
var
ERROR
at
line
1:
ORA-00904:
"VOUCHER":
invalid
identifier

I think the source of the problem is the single quotes surrounding the
tale name
I can't tell why this is happening or how to solve it

PLEASE HELP!!!
This issue is really plaguing me :-(

Thanks
hyperboogie [ Do, 22 November 2007 15:08 ] [ ID #1876351 ]

Re: sql statement in a here document - 2nd try

hyperboogie wrote:
> Hello all
>
> I have posted this question before but no one seemed to know what the
> problem was or how to solve it, so I'm giving it another try ..
>
> I'm having some problems with an sql statement in a here document.
> The following code works fine:
> #!/bin/ksh
> .
> .
> result=$(sqlplus -s myuser/mypass [at] myinstance << EOF
> select tname from tab
> /
> EOF)
>
> echo $result | tr " " "\n"
>
>
> but when I add a "where field like 'XXX' " statement with single
> quotes, such as:
> #!/bin/ksh
> .
> .
> result=$(sqlplus -s myuser/mypass [at] myinstance << EOF
> select tname from tab where tname like 'VOUCHER'
> /
> EOF)
>
>
>
> I get the the content of the current directory and an ORACLE error
> message, for example:
> .
> .
> sqlnet.log
> testing
> users
> var
> ERROR
> at
> line
> 1:
> ORA-00904:
> "VOUCHER":
> invalid
> identifier
>
> I think the source of the problem is the single quotes surrounding the
> tale name
> I can't tell why this is happening or how to solve it
>
> PLEASE HELP!!!
> This issue is really plaguing me :-(
>
> Thanks

Not sure, but try quoting EOF. This has the effect of
suppressing shell processing of the here doc body.
While quotes don't seem to get stripped for me using
bash, maybe in ksh it does.

result=$(sqlplus -s myuser/mypass [at] myinstance << \EOF
select tname from tab where tname like 'VOUCHER'
/
EOF)

Also, you should quote $result in the echo statement,
instead of using tr:
echo "$result"
Or even better:
printf '%s\n' "$result"

-Wayne
wayne [ Do, 22 November 2007 16:14 ] [ ID #1876353 ]

Re: sql statement in a here document - 2nd try

Have you tried '%VOUCHER%'? Just an idea.
buffer0verflow [ Do, 22 November 2007 16:55 ] [ ID #1876354 ]

Re: sql statement in a here document - 2nd try

hyperboogie wrote:
[...]
> but when I add a "where field like 'XXX' " statement with single
> quotes, such as:
> #!/bin/ksh
> .
> .
> result=$(sqlplus -s myuser/mypass [at] myinstance << EOF
> select tname from tab where tname like 'VOUCHER'
> /
> EOF)
>
> echo $result | tr " " "\n"
>
> I get the the content of the current directory and an ORACLE error
> message, for example:
> .
> .
> sqlnet.log
> testing
> users
> var
> ERROR
> at
> line
> 1:
> ORA-00904:
> "VOUCHER":
> invalid
> identifier
[...]

Try with:

t="'VOUCHER'"
result="$(sqlplus -s myuser/mypass [at] myinstance << EOF
set pages 0 feed off
select tname from tab where tname like $t
/
EOF)"


Dimitre
cichomitiko [ Do, 22 November 2007 17:15 ] [ ID #1876355 ]
Linux » comp.unix.shell » sql statement in a here document - 2nd try

Vorheriges Thema: exec in ksh
Nächstes Thema: rm < junkfilelist.txt