Unix & Linux: Assigning the output of a SQL query to variable (2 Solutions!!)
Channel: Roel Van de Paar
Date: 2020-08-10

Unix & Linux: Assigning the output of a SQL query to variable


The Question: I am connecting to oracle database and firing a query and assigning the output
to variable But when I echo the value of the variable it doesn't get printed
correctly.
count=`sqlplus -s $configuser/$configpass@$ORACLE_SID ＜＜END
 set pagesize 0 feedback off verify off heading off echo off;
 select max(cust_id) from cutomers
 exit;
 END`
 echo $count
The query return correct result when fired on database. But "count" variable
has incorrect value.

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 13 people ==
The terminating here-doc word must be the only characters on the line: no
indenting allowed. Also, use $() instead of backticks -- they are nestable.
count=$(sqlplus -s $configuser/$configpass@$ORACLE_SID ＜＜END
set pagesize 0 feedback off verify off heading off echo off;
select max(cust_id) from cutomers
exit;
END
)
echo $count
http://www.gnu.org/software/bash/manual/bashref.html#Here-Documents

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user Tathagata Mukherjee (https://unix.stackexchange.com/users/116774), user suni (https://unix.stackexchange.com/users/44029), user peterh - Reinstate Monica (https://unix.stackexchange.com/users/52236), user glenn jackman (https://unix.stackexchange.com/users/4667), user Gilles 'SO- stop being evil' (https://unix.stackexchange.com/users/885), and the Stack Exchange Network (http://unix.stackexchange.com/questions/85180). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.