Sponsored Content
Top Forums Shell Programming and Scripting how to return an array of elements from oracle to shell script Post 79689 by tmarikle on Friday 29th of July 2005 03:16:01 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PG_FETCH_ROW(3) 														   PG_FETCH_ROW(3)

pg_fetch_row - Get a row as an enumerated array

SYNOPSIS
array pg_fetch_row (resource $result, [int $row]) DESCRIPTION
pg_fetch_row(3) fetches one row of data from the result associated with the specified $result resource. Note This function sets NULL fields to the PHP NULL value. PARAMETERS
o $result - PostgreSQL query result resource, returned by pg_query(3), pg_query_params(3) or pg_execute(3) (among others). o $row - Row number in result to fetch. Rows are numbered from 0 upwards. If omitted or NULL, the next row is fetched. RETURN VALUES
An array, indexed from 0 upwards, with each value represented as a string. Database NULL values are returned as NULL. FALSE is returned if $row exceeds the number of rows in the set, there are no more rows, or on any other error. EXAMPLES
Example #1 pg_fetch_row(3) example <?php $conn = pg_pconnect("dbname=publisher"); if (!$conn) { echo "An error occurred. "; exit; } $result = pg_query($conn, "SELECT author, email FROM authors"); if (!$result) { echo "An error occurred. "; exit; } while ($row = pg_fetch_row($result)) { echo "Author: $row[0] E-mail: $row[1]"; echo "<br /> "; } ?> SEE ALSO
pg_query(3), pg_fetch_array(3), pg_fetch_object(3), pg_fetch_result(3). PHP Documentation Group PG_FETCH_ROW(3)
All times are GMT -4. The time now is 01:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy