SQL select statement in UNIX inside/within IF..THEN statement SQL select statement in UNIX inside/within IF..THEN statement unix unix

SQL select statement in UNIX inside/within IF..THEN statement


This sounds like you're trying to embed SQLPlus in a shell script. From memory the incantation should look something like:

if [ $? -eq ABC ]; thenSQLPLUS /S USER/PASS@Instance <<EOFSET echo off;SET pagesize 0;SET heading off;SPOOL foo.outselect foo from barEOFfi

Everything between the SQLPLUS and EOF is passed to SQLPlus, so we have some statements to control the formatting (you may want different ones) and the actual query. The SPOOL command in the SQLPlus script sends the output to a file. For more detailed docs on using SQLPlus, You can download them from Oracle's web site.


Remember that echo is your friend.

if [ "$?" = "ABC" ] then echo SELECT employid, name, age FROM tablename; else exit 1; fi

You can embed shell script variables and such. Be careful of the need to quote things the shell wants to act upon, like quotes and semi-colons.


Have you considered using perl or other scripting language that includes database connection functionality. That way you avoid the clunky shell script/SQL*Plus linkage