Passing Shell array to SQLPlus


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing Shell array to SQLPlus
# 8  
Old 12-08-2011
It is still not clear to me what you want, but let see: you want to execute sqlplus for every Id you find, is that right?
Code:
$ cat testScript.sh
execSQLPlus ()
{
	deptNo="${1}"
	sqlplus -S -L /nolog <<EOF
		whenever sqlerror exit 1
		connect user@database/password
		set serveroutput on size 1000000
		variable deptno number;
		exec :deptno := '${deptNo}'
		select customer_name from customer where customer_id=:deptno;
EOF
	sqlplusRetCode=$?
	if [ ${sqlplusRetCode} -ne 0 ]
	then
		echo "Failed to execute query for deptno: [${deptNo}]. Return code: [${sqlplusRetCode}]."
		return ${sqlplusRetCode}
	fi
	return 0
}

arrayCountPos=0
arrayLastPos=${#FileName[@]}
while [ ${arrayCountPos} -lt ${arrayLastPos} ]
do
	currValue="${FileName[${arrayCountPos}]}"
	
	execSQLPlus "${currValue}"
	
	arrayCountPos=`expr ${arrayCountPos} + 1`
done

I don't have how to test it now, but it should work!
# 9  
Old 12-08-2011
That's too much close to what I'm looking for,
The only point left is i do need to open the connection one time for each array

is it possible to wave
connect user@database/password
to be connected before the loop starts, and close the connection after the whole loop ends
# 10  
Old 12-08-2011
In shell, I don't know a way to keep an Oracle connection opened (and I don't think there is a way, sqlplus does not support it!).

By the way, if you want this, why the first piece of code I wrote is not valid to you? It executes all queries in the FileName array sequentially, in the same connection!

If you really need to open the connection once, execute the queries and then close the connection, I suggest you to use another programming language.

My suggestion is: Python and its lib to connect to Oracle: cx_Oracle

Here is an example: Mastering Oracle+Python, Part 1: Querying Best Practices
# 11  
Old 12-08-2011
Sorry but it is not clear to me at which point in the first code you are opening the connection.
Kindly repeat it marking the line for opening the connection.
# 12  
Old 12-08-2011
What is your OS and what shell are you using?
# 13  
Old 12-08-2011
Quote:
Originally Posted by roby2411
...
I have a system that is throwing number of id's in files where each file has more than one line and each line has one id
right now i have a code to cat the files each 5 minute and pass each file content to the given array FileName -the array in my code is just sample for the dynamic output and it is not static like shown- and i do shell loop to do the following
  1. select the next value from shell array using the loop number as index
  2. open sql connection
  3. run query using sqlplus bind variable and query about the given value from shell array
  4. close the connection
all what i'm looking for is to keep the whole logic but loop after opening the sqlplus because this change will offload the oracle resources somehow as opening connection is considered a high cost action for resources
...
You could loop through the array, generate all SQL statements, assign that to a shell variable and then use that variable in the sqlplus connection.
Thus, you move the task of generating your statements to Unix and open only one connection to Oracle when you're ready with your batch of queries.

Code:
$
$
$ cat -n oraconnect.sh
  1  #!/usr/bin/bash
  2
  3  # Let's say the array has been populated at this point
  4  # Over here, I've populated the empno values from the EMP
  5  # demonstration table of the SCOTT sample schema of Oracle
  6  id_array[1]=7369
  7  id_array[2]=7839
  8  id_array[3]=7900
  9  id_array[4]=7654
 10
 11  # Generate all SQL statements by looping through the array
 12  # and assign that to a single shell variable
 13  SQLSTR=$(for i in "${id_array[@]}"; do
 14             echo "SELECT ename from emp where empno = $i;"'\n'
 15           done)
 16
 17  # now connect to Oracle and execute the SQL statements in
 18  # the string variable
 19  sqlplus -s /nolog <<EOF
 20  connect user/password@db
 21  set pages 0 heading off time off timing off feed off
 22  $(echo -e $SQLSTR)
 23  exit
 24  EOF
 25
$
$
$ ./oraconnect.sh
SMITH
KING
JAMES
MARTIN
$
$
$

Now for the part that says that the array is dynamic and is actually being created by reading text files. In that case, you do not need an array.
You loop through the files, read the ids, generate the queries all the while appending them, assign the value to a Unix variable and then use it while interacting with Oracle.

Code:
$
$
$ # The Ids are in the files - file1 and file2
$
$ cat file1
7369
7839
$
$ cat file2
7900
7654
$
$
$ cat file1 file2
7369
7839
7900
7654
$
$
$ cat -n oraconnect1.sh
  1  #!/usr/bin/bash
  2
  3  # If the Ids are being fetched from files that have one Id
  4  # per line, then an array is not really needed.
  5  SQLSTR=$(cat file? | while read i; do
  6                         echo "SELECT ename from emp where empno = $i;"'\n'
  7                       done)
  8
  9  # now connect to Oracle and execute the SQL statements in
 10  # the string variable
 11  sqlplus -s /nolog <<EOF
 12  connect user/password@db
 13  set pages 0 heading off time off timing off feed off
 14  $(echo -e $SQLSTR)
 15  exit
 16  EOF
 17
$
$
$ ./oraconnect1.sh
SMITH
KING
JAMES
MARTIN
$
$
$

tyler_durden

---------- Post updated at 11:51 AM ---------- Previous update was at 11:28 AM ----------

In fact, depending on what your queries are and how you want to use their output, you might be able to use the IN operator, like so -

Code:
$
$ # For the text files - "file1" and "file2" posted earlier...
$
$ cat -n oraconnect2.sh
  1  #!/usr/bin/bash
  2
  3  # If the Ids are being fetched from files that have one Id
  4  # per line, then an array is not really needed.
  5  SQLSTR=$(cat file? |
  6           xargs printf "%s, " |
  7           sed 's/^\(.*\), $/SELECT ename FROM emp WHERE empno IN (\1);/')
  8
  9  echo "My query is: $SQLSTR"
 10
 11  # now connect to Oracle and execute the SQL statements in
 12  # the string variable
 13  sqlplus -s /nolog <<EOF
 14  connect user/password@db
 15  set pages 0 heading off time off timing off feed off
 16  $(echo -e $SQLSTR)
 17  exit
 18  EOF
 19
$
$
$ ./oraconnect2.sh
My query is: SELECT ename FROM emp WHERE empno IN (7369, 7839, 7900, 7654);
SMITH
MARTIN
KING
JAMES
$
$

tyler_durden

Last edited by durden_tyler; 12-08-2011 at 06:13 PM..
# 14  
Old 12-11-2011
Thank you durden
the last part of code is what I was looking for, but there is another issue now. using this way will use the sql bind variables.

I still need to use bind variables from oracle to execute a pre defined shell array
And sorry for the delay in answering and give feedback
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing string from bash to sqlplus

Hello, I have file (PARFILE) with string on first line: INCLUDE=SCHEMA:"IN\( 'SCHEMA1','SCHEMA2','SCHEMA3' \)"In .sh script I use: .... IMPORT_SCHEMA=`awk 'NR==1{print $2}' ${PARFILE}` ...print $2 is because 'SCHEMA1','SCHEMA2','SCHEMA3' is 2nd column in file echo "$IMPORT_SCHEMA"... (5 Replies)
Discussion started by: DjukaZg
5 Replies

2. Shell Programming and Scripting

Passing Oracle function as file input to sqlplus

Apologies if this is the incorrect forum.There is an issue in the function call I am facing while calling the same from a unix shell scripts. Basically, I want the ref cursor to return values to a variable in sqlpus. The function call is currently saved in a ".txt" file in a unix location. I want... (7 Replies)
Discussion started by: amvip
7 Replies

3. Shell Programming and Scripting

Passing sqlplus output to shell variable

Hi , I am using below code : for i in `ps -ef|grep pmon|awk {' print $8 '}|cut -f3 -d'_'|grep -v '^grep'` do ORACLE_SID=$i export ORACLE_SID; dest=`sqlplus "/ as sysdba" <<EOF set heading off feedback on verify off select DESTINATION from v\\$archive_dest where target in... (5 Replies)
Discussion started by: admin_db
5 Replies

4. Shell Programming and Scripting

Passing a parameter from a shell script to sqlplus

Hi All, I'm new to Linux and scripting, apologies in advance for 'stupid' questions. Please help... Im writing a script that calls a sqlplus script but the sqlplus requires inputs and i cant seem to get this to work. here is my code. #!/bin/sh TERM=vt100 export TERM... (4 Replies)
Discussion started by: Mahomed
4 Replies

5. Shell Programming and Scripting

Shell Script passing parameters to sqlplus code

Hello All, I am interested in finding out a way to pass parameters that are entered at the prompt from HP unix and passed to SQLPlus code with a Shell Script. Is this possible? Thanks (4 Replies)
Discussion started by: compprog11
4 Replies

6. Shell Programming and Scripting

Passing a file handler and an array from Perl to Shell Script

Hi there, I am trying to call a shell script from a Perl script. here is the code: @args = ("sh", "someshellprg.sh", "a file handler", "an array"); system(@args) == 0 or die "system @args failed: $?"; in the shell program, I examine if the arguments exits using: if then echo... (5 Replies)
Discussion started by: pinkgladiator
5 Replies

7. Shell Programming and Scripting

Passing the unix variable to sqlplus

Hi, I am writing a script which creates an external table using a shell script. My requirement is like this. Usage: . ./r.ksh <table_name> - this should create an external table. e.g . ./r.ksh abc - this should create an external table as abc_external. How do i achieve this? Please... (5 Replies)
Discussion started by: Anaramkris
5 Replies

8. Shell Programming and Scripting

error in passing a variable to sqlplus from a shell script

hi, I am using a shell script from where i will be conecting to sqlplus.. i am having a problem in passing a variable to sqlplus query.. i will be assigning the variable in the unix environment..whenever i am trying to pass a variable having the contents greater than 2500 characters, i am... (3 Replies)
Discussion started by: kripssmart
3 Replies

9. UNIX for Advanced & Expert Users

passing unix variable to sqlplus without a file name

Hi, I want to input unix variable to sqlplus.The following is working fine sqlplus username/password @dummy.sql param1 param2 << EOF create user $1 identified by $2; EOF But I dont want any file name to be passed,I just want to pass the parameter. Is there any way to that?? Thanks... (3 Replies)
Discussion started by: sakthi.abdullah
3 Replies

10. Shell Programming and Scripting

passing parameters from a shell script to sqlplus

Hi , I want to pass parameters from a shell script to a sql script and use the parameter in the sql query ..and then I want to spool a particular select query on to my unix box... for 4 different locations by writing only one sql script Right now no file is generated on the unix box...it is a... (2 Replies)
Discussion started by: phani
2 Replies
Login or Register to Ask a Question