How do you run a SQL command in a shell script while setting a variable? I had tried this method and it isn't executing the command, it is thinking the command is just a string.
#!/bin/ksh
variable1=`sqlplus -s username/pw@oracle_instance <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
select count(*) from table;
EXIT;
EOF`
if [ -z "$variable1" ]; then
echo "No rows returned from database"
exit 0
else
echo $variable1
fi
Code
Explanation:
PL_CONNECT_STRING
carry database username, password and it service namesqlplus is used to connect the Database with
PL_CONNECT_STRING
detailsEND-OF-SQL
tag contain the query which you want to executeecho
is used to print the output of the queryNOTE: You can give multiple Query inside the
END-OF-SQL
tag, so its useful for batch execution as wellMaybe it's too late for answering but, there's a working code:
You can use a heredoc. e.g. from a prompt:
so
sqlplus
will consume everything up to theEOF
marker as stdin.