How to run SQL in shell script How to run SQL in shell script unix unix

How to run SQL in shell script


#!/bin/kshvariable1=$( echo "set feed offset pages 0select count(*) from table;exit"  | sqlplus -s username/password@oracle_instance)echo "found count = $variable1"


You can use a heredoc. e.g. from a prompt:

$ sqlplus -s username/password@oracle_instance <<EOFset feed offset pages 0select count(*) from table;exitEOF

so sqlplus will consume everything up to the EOF marker as stdin.


Maybe it's too late for answering but, there's a working code:

sqlplus -s "/ as sysdba" << EOF    SET HEADING OFF    SET FEEDBACK OFF    SET LINESIZE 3800    SET TRIMSPOOL ON    SET TERMOUT OFF    SET SPACE 0    SET PAGESIZE 0    select (select instance_name from v\$instance) as DB_NAME,           file_name      from dba_data_files     order by 2;