how to return an array of elements from oracle to shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to return an array of elements from oracle to shell script
# 1  
Old 07-29-2005
MySQL how to return an array of elements from oracle to shell script

Hi all,

I have a interresting problem. My application is as follows:

From a shell script i will conn to a oracle database and fetch few rows using a select statement.

Then i want to sort these rows and return a column (collection of values of a column from the selected results) as array back to the shellscript from which i conn to the database.

p.s. I don't want to redirect the result set into a file and use it

Please help me out in solving this problem

Thanks in advance

best regards
satya
# 2  
Old 07-29-2005
Quote:
Originally Posted by satyakiran
From a shell script i will conn to a oracle database and fetch few rows using a select statement.

Then i want to sort these rows and return a column (collection of values of a column from the selected results) as array back to the shellscript from which i conn to the database.

p.s. I don't want to redirect the result set into a file and use it
I prefer to use a co-process to give me maximum control over result sets. This is one method that should help you craft a solution that meets the requirement (as I read it):

Code:
#! /usr/bin/ksh

# Create co-process command processor
sqlplus -S user/password@db |&

# Send SQL to co-process
# Note: FOLD_AFTER causes all columns in the row to be
# printed as individual rows
# Also note the "sql complete" prompt which acts 
# as a marker to end the read loop.  Otherwise
# the loop will expect more output from Oracle.
print -p "
SET FEEDBACK OFF VERIFY OFF ECHO OFF PAGES 0
COLUMN Array_Element FOLD_AFTER
SELECT 'some_start_of_row_marker' Array_Element
      ,'Owner:'||owner Array_Element
      ,'Table:'||table_name Array_Element
      ,'some_end_of_row_marker' Array_Element
FROM all_tables
WHERE ROWNUM < 5
/
PROMPT sql complete
"
# Make certain that "read" reads the whole line of text
IFS='
'
# Loop through the results
while read -p LINE
do
    case "$LINE" in
        # Look for marker and break loop
        sql\ complete) 
            break 
        ;;

        ORA-*|SP2-*)
            print "Error occured"
            print "$LINE"
        ;;

        # Column 1; reset array
        some_start_of_row_marker*) 
            print "Construncting new array" 
            set -A the_array
            x=0
        ;;

        # Last column of row; process logic using array
        some_end_of_row_marker*) 
            print "Row finished; printing array containing row's data"
            for i in "${the_array[@]}"
            do
               print $i | nawk -F: '{printf ("%-10s:\t%-30s\n", $1, $2)}'
            done
            print
        ;;
 
        # All other lines are columns between start and end markers
        *) the_array[$x]=$LINE 
           (( x = $x + 1 ))
        ;;
    esac
done

Here are the results:
Code:
Construncting new array
Row finished; printing array containing row's data
Owner     :     SYS                           
Table     :     DUAL                          

Construncting new array
Row finished; printing array containing row's data
Owner     :     SYS                           
Table     :     SYSTEM_PRIVILEGE_MAP          

Construncting new array
Row finished; printing array containing row's data
Owner     :     SYS                           
Table     :     TABLE_PRIVILEGE_MAP           

Construncting new array
Row finished; printing array containing row's data
Owner     :     SYS                           
Table     :     STMT_AUDIT_OPTION_MAP

The co-process can be used over and over and stays active throughout your script's execution.

Thomas
# 3  
Old 08-02-2005
Hi Thomas,

Thanks a lot for ur reply, i tried it in my system and some how its not working properly the errors i got are as follows

temp.sh: line 24: syntax error near unexpected token `&'
temp.sh: line 24: `sqlplus -S $ora_userid/$ora_pwd@$ORACLE_SID |&'

this error is disappearing if remove |& part from the above mentioned line.

please help me out in knowing this. if there is any other way please let me know

Thanks in advance
satya
# 4  
Old 08-02-2005
What shell are you using. The example is Korn shell. It won't work in bash.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Scanning array for partial elements in Bash Script

Example of problem: computerhand=(6H 2C JC QS 9D 3H 8H 4D) topcard=6D How do you search ${computerhand} for all elements containing either a "6" or a "D" then save the output to a file? This is a part of a Terminal game of Crazy 8's that I'm attempting to write in Bash. Any... (2 Replies)
Discussion started by: cogiz
2 Replies

2. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

3. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

4. HP-UX

return code from oracle to unix script

Hi I'm writing a shell script that connects to oracle database and fires query to check the availability of data in a table. In case of no data found then what will be the return code and how to handle in that in variable. Kindly provide with an example for better understanding... Thanks... (1 Reply)
Discussion started by: ksailesh
1 Replies

5. UNIX for Dummies Questions & Answers

Return value from oracle to unix shell

Hi , i have written a shell scipt to call a procedure and get the value back from procedure.But i am facing the issue like #!/bin/sh returnedvalue=`sqlplus -s userid/pass<<EOF set serveroutput on; exec pass($1) set serveroutput off; EXIT; EOF` flag=`echo $returnedvalue ` echo "$flag"... (2 Replies)
Discussion started by: ravi214u
2 Replies

6. Shell Programming and Scripting

Displaying Array Elements in Shell Scripts

Hi All, I am using the following piece of script to print all the array elements in a script by name compare.sh: 31 len=${#array }; 32 j=0; 33 #echo "The length of the array is : $len" 34 while ; do 35 temp=${array} 36 echo "$temp" 37 let $j++ 38 done But I am getting the... (2 Replies)
Discussion started by: ananddr
2 Replies

7. Shell Programming and Scripting

To return the elements of array

Hi, Please can someone help to return the array elements from a function. Currently the problem I face is that tempValue stores the value in myValue as a string while I need an array of values to be returned instead of string. Many Thanks, Sudhakar the function called is: ... (5 Replies)
Discussion started by: Sudhakar333
5 Replies

8. Shell Programming and Scripting

Capture Oracle return code in shell script

I am using the following code in my shell script list=`sqlplus -s $user/$pwd@$dbms<<EOF WHENEVER SQLERROR EXIT SQL.SQLCODE set pagesize 0 feedback off verify off heading off echo off select * from control_tbl where src_nm=$3 and extrct_nm=$4; exit SQL.SQLCODE; EOF` ERROR=$?... (1 Reply)
Discussion started by: Vikas Sood
1 Replies

9. Shell Programming and Scripting

Store return code of shell script in oracle table

Hi friends, I have to do the following things : 1) there should be a shell script returning the returning the return code of the script. and i have to add some more details like on which machine is has run , at what time and other details and then using plsql i have to add a row to Oracle... (3 Replies)
Discussion started by: sveera
3 Replies

10. UNIX for Advanced & Expert Users

Oracle To Korn Shell Array

I'm attempting to populate an array in ksh using the following command: set -A $(SELECT_INVOICE | sed '/^$/d') SELECT_INVOICE is a function that executes the SQL query. Problem: Some of the invoice numbers have alpha characters with spaces(example: OVEN MICRO). The Korn shell is treating... (1 Reply)
Discussion started by: kdst
1 Replies
Login or Register to Ask a Question