Assign output of sql query to a variable in unix

19,798

You need exit also:

count=`sqlplus -s ${DB_USER}/${DB_PASS}@${DB_INST} <<END
set pages 0 echo off feed off
SELECT COUNT(column_name)
exit;
END`
Share:
19,798
Shitu
Author by

Shitu

Software Engineer

Updated on June 04, 2022

Comments

  • Shitu
    Shitu almost 2 years

    I am using the following simple UNIX script to assign the output to a variable.

    count=`sqlplus -s ${DB_USER}/${DB_PASS}@${DB_INST} << END
            SELECT COUNT(column_name) from table_name;
    END`
    
    echo $count
    

    But I am getting the following error on executing:

    SP2-0042: unknown command "END" - rest of line ignored.
    

    count=`sqlplus -s ${DB_USER}/${DB_PASS}@${DB_INST} << END

    When I tried to execute the above statement in putty, it was saying as "bad substitution"

    So I am using UNIX script to assign the output to a variable.

    count=sqlplus DB_USER/DB_PASS << END SELECT VERSION_NUMBER from GA_PERIODIC_REFRESH where MODULE_NAME in 'RoaminfoService'; exit; END echo $count