Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Get Oracle fuction return value in a variable Post 69403 by tmarikle on Thursday 14th of April 2005 12:50:34 PM
Old 04-14-2005
If you want more...

Not to beat a dead horse but if you want scads (more than one value) of stuff returned from Oracle you can utilize my favorite method:

Code:
#! /bin/ksh
...
# Create a SQL*Plus command processor co-process
sqlplus -s /nolog |&

# Connect to the database
print -p connect username/password@database

# Send SQL or PL/SQL block
print -p "
REM get_parameters()
SET HEAD OFF FEEDBACK OFF VERIFY OFF <- Set whatever formatting options you wish
COLUMN param FOLD_AFTER              <- This will cause all columns named param to be printed as individual rows
  SELECT 'Input_Record_Size:'   || input_record_size_no    param <- All columns are named param
        ,'Output_Record_Size:'  || output_record_size_no   param
        ,'Convert_to_ASCII:'    || convert_to_ascii_fl     param
        ,'Staging_Table:'       || load_table_name         param
        ,'SQLLoad_Control_File:'|| sqlload_control_file    param
    FROM load_parameters
   WHERE system_code  ='${SYSTEM_CD}' <- Pass whatever criteria you need
     AND file_type    ='${FILE_TYPE}'
ORDER BY load_priority
/
SET HEAD ON FEEDBACK ON VERIFY ON
COLUMN param CLEAR
PROMPT sql_complete <- This is very important.  If you don't print an arbitrary message that you can search for, the while loop below will hang while waiting for more information from Oracle
"

# Get responses back from Oracle
while read -p LINE
do
    # Look for our loop termination marker and break out when found
    if print $LINE | grep sql_complete
    then
        break

    # Process any Oracle errors
    elif print $LINE | grep Oracle error message(s)
    then
        print "Oracle error occurred"
        exit 1

    # Start setting variables based on return values
    elif print $LINE | grep "Input_Record_Size:"
    then
        INPUT_RECORD_SIZE=$(print $LINE | awk -F: '{print $2}')
        continue
    elif print $LINE | grep "Output_Record_Size:"
    then
        OUTPUT_RECORD_SIZE=$(print $LINE | awk -F: '{print $2}')
        continue
    ...more tests...
    fi
done

print "Input record size is: ${INPUT_RECORD_SIZE}"
print "Output record size is: ${OUTPUT_RECORD_SIZE}"
...
# Now a PL/SQL block
print -p "
REM Invoke stored procedure
SET HEAD OFF VERIFY OFF FEEDBACK OFF SERVEROUTPUT ON
DECLARE
    PRAGMA AUTONOMOUS_TRANSACTION;
    v_rc PLS_INTEGER;
BEGIN
    v_rc := please_raise_my_salary (${MY_ID});
    COMMIT;
    dbms_output.put_line('RC=:' || v_rc);
END;
/
SET HEAD ON VERIFY ON FEEDBACK ON SERVEROUTPUT OFF
PROMPT sql_complete
"

while read -p LINE
do
    if print $LINE | grep "sql_complete"
    then
        break
        
    ...same error tests as before...

    # Test for stored procedure return value
    elif print $LINE | grep "RC="
    then
        RETURN_VALUE=$(print $LINE | awk -F= '{print $2}')
        if [ 0 -eq ${RETURN_VALUE} ]
        then
            print "Finally, now I can buy another pair of shoes!"
        else
            print "Maybe I can have them re-soled"
        fi
    fi
done

I will keep the co-process available throughout my code and send SQL and PL/SQL blocks at various times.

Thomas

Last edited by tmarikle; 04-14-2005 at 02:03 PM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

return code from oracle

I am writing a unix script that logs into oracle and does a count on a table. I want to return that count to my unix script. this is my script: #!/bin/ksh typeset retcode; numin=0 sqlplus -s <<-EOSQL myuser/mypass@mydb variable ret_val number; begin ... (3 Replies)
Discussion started by: lesstjm
3 Replies

2. Shell Programming and Scripting

ORACLE return a function to Solaris

Guys, I´m have this problem and I do not know what to do anymore: cod=`sqlplus -s ${DATABASE} << EOF set heading off feedback off verify off select max(eventid) from events; exit EOF` sed "s/CODEVENTID/${cod}/" c.ctl Abova What I´ve done. The CODEEVENTID is already set into a... (1 Reply)
Discussion started by: Rafael.Buria
1 Replies

3. Shell Programming and Scripting

fuction return in perl

Hi All, I have a perl script(1.pl) that calls a c function defined in another file sample.c #!/usr/bin/perl my $re = 1; my @s = `/home/PERL_SCRIPTING/Rough/sample pline $re 10`; print "$_" foreach(@s); The sample.c is as bwlow: # include <stdio.h> int pline(int, int);... (4 Replies)
Discussion started by: jisha
4 Replies

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

5. Shell Programming and Scripting

Return rows from Oracle package

I need help . I am not getting anything back from my setup. I have defined an oracle package which I am calling from within my sql call, but is returning no rows. If I run my sql from sqlplus works find, does not work whe I try to run from UNIX shell script My korn script test.ksh #!bin/ksh... (6 Replies)
Discussion started by: TimHortons
6 Replies

6. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

7. UNIX and Linux Applications

Oracle return codes?

Having searched high and low through Oracles documentation, I came to think that they're very scripting-averse, as there's (apparently) no list of possible return/exit codes for their various command line utilities. Is anyone here in possession of such a list, or knows where to find one? It... (16 Replies)
Discussion started by: pludi
16 Replies

8. Shell Programming and Scripting

getting return value from oracle function

So, in Oracle (11gR2) on aix I have a function like: CREATE OR REPLACE function COMMON.t2(var1 in varchar2, vari in number,var2 in out number) return number as begin insert into korisnik_temp(kor_inicia, kor_opisno) values(1, var1); var2:=0; return var2; exception when... (12 Replies)
Discussion started by: bongo
12 Replies

9. UNIX and Linux Applications

Identify a specific environment Oracle variable to connect a remote Oracle database ?

Good evening I nned your help pls, In an unix server i want to connect to a remote oracle databse server by sqlplus. I tried to find out the user/passwd and service name by env variable and all Ive got is this: ORACLE_SID_REPCOL=SCL_REPCOL ORACLE_SID=xmeta ORACLE_SID_TOL=SCL_PROTOLCOL... (2 Replies)
Discussion started by: alexcol
2 Replies
MSSQL_QUERY(3)															    MSSQL_QUERY(3)

mssql_query - Send MS SQL query

SYNOPSIS
mixed mssql_query (string $query, [resource $link_identifier], [int $batch_size]) DESCRIPTION
mssql_query(3) sends a query to the currently active database on the server that's associated with the specified link identifier. PARAMETERS
o $query - An SQL query. o $link_identifier - A MS SQL link identifier, returned by mssql_connect(3) or mssql_pconnect(3). If the link identifier isn't specified, the last opened link is assumed. If no link is open, the function tries to establish a link as if mssql_connect(3) was called, and use it. o $batch_size - The number of records to batch in the buffer. RETURN VALUES
Returns a MS SQL result resource on success, TRUE if no rows were returned, or FALSE on error. EXAMPLES
Example #1 mssql_query(3) example <?php // Connect to MSSQL $link = mssql_connect('KALLESPCSQLEXPRESS', 'sa', 'phpfi'); if (!$link || !mssql_select_db('php', $link)) { die('Unable to connect or select database!'); } // Do a simple query, select the version of // MSSQL and print it. $version = mssql_query('SELECT @@VERSION'); $row = mssql_fetch_array($version); echo $row[0]; // Clean up mssql_free_result($version); ?> NOTES
Note If the query returns multiple results then it is necessary to fetch all results by mssql_next_result(3) or free the results by mssql_free_result(3) before executing next query. SEE ALSO
mssql_select_db(3), mssql_connect(3). PHP Documentation Group MSSQL_QUERY(3)
All times are GMT -4. The time now is 03:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy