How to avoid ISQL error?

I am using ISQL command in my script like below

echo "going to execute isql for getting JE count"
isql -Uxyz -SSYB_LOCOMOTIVE_xx -Pabc -Dlmn -w300 -o$sumOfJEOutputFile <<EOF
select sum(count(*)) from lmn..ex_je_t where clm_id in ($(cat $finalTemp)) group by clm_id
go
EOF
retval=$?
cat $retval
if [[ $retval != 0 ]]; then
                echo "Failed to connect to the Database" | mail -s "ERROR OCCURED:Database connection Failed" "$EXPRS_SUPPORT" 
                exit 0
else
                echo "Database connection successful."
fi
echo "coming out of isql command"
echo "#################################### output of isql command for getting the sum of JEs ##################################################"
cat $sumOfJEOutputFile

I used the if condition so that when there is an error i can exit the script with a message.

But this is not happening,instead i am getting the error printed in my output file.Like this:

Msg 102, Level 15, State 1:
Server 'SYB_LOCOMOTIVE_JE', Line 1:
Incorrect syntax near ')'.

I wonder how the return value of ISQL is 0 when i am getting error instead of proper Output.

Can someone help on this?

Not tested but try to add retserverror to your isql command:

isql -Uxyz -SSYB_LOCOMOTIVE_xx -Pabc -Dlmn -w300 -o$sumOfJEOutputFile --retserverror

By default it prints the message (server errors) to STDOUT.
You can invoke the isql with --retserverror option enabled to get STDERR.

isql -Uxyz -SSYB_LOCOMOTIVE_xx -Pabc -Dlmn -w300 -o$sumOfJEOutputFile --retserverror <<EOF

---------- Post updated at 16:27 ---------- Previous update was at 16:23 ----------

Found more info from manual page:

--retserverror
Forces isql to terminate and return a failure code when it encounters a server error of severity greater than 10. 
When isql encounters this type of abnormal termination, it writes the label �Msg� together with the actual Adaptive Server error number to stderr, 
and returns a value of 2 to the calling program.
isql prints the full server error message to stdout.