Tuesday, July 29, 2008

Scripting Oracle SQL

To run sql commands of oracle in a script to use abilities of it like scheduling with crontab write the shell script commands in script.sh as belows

script.sh:
FILENAME=$(date +'%d-%m-%Y.txt')
sqlplus user/pass@db-sid <<EOL >> $FILENAME
select * from tbl1 where rownum<10000;
EOL

If sqlplus can't be run in the context of the this user the above script can be called in another script with su -c or specifying oracle user in crontab.