Find the answer to your Linux question:
Results 1 to 4 of 4
Hi all, I need to know if it's possible to pass variables from a shell script to sqlplus. The script reads a Table Name from user input & executes a ...
  1. #1
    Just Joined!
    Join Date
    Jan 2009
    Posts
    2

    Pass Variables from Shell to Sql

    Hi all,
    I need to know if it's possible to pass variables from a shell script to sqlplus.
    The script reads a Table Name from user input & executes a Select statement based on
    that input.

    Script:
    Code:
    ....
    echo "Enter table name: \c "
    read answer
    
    sqlplus 'oracle/*** ' <<!
          spool columns.sql
          @select.sql
          spool off
    exit
    !
    Inside script SELECT.SQL , i need to use the variable "answer" to run the query.
    ($answer doesn't do the job...)
    Any help would be highly appreciated.
    Thanks in advance...

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    You are already using a here document to provide sql statements to the standard input of sqlplus. Instead of using "@select.sql" to include that file, you could include the statements it contains, and use variables in these statements, e.g.:

    Code:
    echo "Enter table name: \c "
    read answer
    
    sqlplus 'oracle/*** ' <<END
          spool columns.sql
          select whatever from wherever where some_value=$answer
          spool off
          exit
    END

  3. #3
    Just Joined!
    Join Date
    Jan 2009
    Posts
    2
    Quote Originally Posted by burschik View Post
    You are already using a here document to provide sql statements to the standard input of sqlplus. Instead of using "@select.sql" to include that file, you could include the statements it contains, and use variables in these statements, e.g.:

    Code:
    echo "Enter table name: \c "
    read answer
    
    sqlplus 'oracle/*** ' <<END
          spool columns.sql
          select whatever from wherever where some_value=$answer
          spool off
          exit
    END
    Thanks for ur reply....
    I know this works, I've already tried it....
    but in my case, if I need the select.sql to be outside the script, how can it work?

  4. #4
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Assuming that "select.sql" contains shell-variable references, like "$answer", for example, you could do something like this:

    Code:
    eval echo  $(&lt; select.sql ) | sqlplus 'oracle/*** '
    IMPORTANT NOTE: You must use the less-than operator rather than &lt; But I was not able to enter that character in the edit field.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...