Skip to Main Content
  • Questions
  • Getting UNIX environment variable in PL/SQL

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Srivatsa.

Asked: December 13, 2000 - 4:16 pm UTC

Last updated: June 08, 2005 - 10:21 pm UTC

Version: 8.0.5.1.0

Viewed 1000+ times

You Asked

helo Tom

Good to see u back

I have a requirement of getting UNIX enviroment variable in pl/sql.

Eg. ORACLE_HOME, HOME etc has certain path. I wish to use this variable (or any other variable which i have defined in UNIX) to retrieve in oracle so that i can use it

LEt me know if you can help me

bi

==srivatsa==


and Tom said...

You can write an EXTPROC (external procedure) to do this and make a call to the C function "getenv". Bear in mind that this will be the SERVERS environment -- never ever the clients (so HOME for example, if set, would always be the HOME of the person who started the database).

See
</code> http://docs.oracle.com/cd/A58617_01/server.804/a58236/10_procs.htm#433731 <code>
for more information on external procedures.



Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Unix shell return Code from DB procedure?

A reader, December 10, 2004 - 2:55 pm UTC

Hi Tom,
I have a situation, where we call a shell sscript from a third party scheduling tool. The shell script further calls a DB procedure. We put an exception in DB procedure to return sqlerrm with raise_application_error function. When we run procedure through shell script, if it fails, we get return code ($?) success, thus scheduling toll indicates - process completed succussfully. Is there any way, when a procedure fails, I get return status <> 0 in the calling shell script?
Thanks

Tom Kyte
December 10, 2004 - 8:11 pm UTC

ops$tkyte@ORA10GR1> whenever
Usage: WHENEVER SQLERROR
     { CONTINUE  [ COMMIT | ROLLBACK | NONE ]
     | EXIT  [ SUCCESS | FAILURE | WARNING | n | <variable> | :<bindvariable> ]
             [ COMMIT | ROLLBACK ] }
 
Usage: WHENEVER OSERROR
     { CONTINUE  [ COMMIT | ROLLBACK | NONE ]  |  EXIT [ SUCCESS | FAILURE |
         WARNING | n | <variable> | :<bindvariable> | OSCODE ]
             [ COMMIT | ROLLBACK ] }
ops$tkyte@ORA10GR1>


<b>
SQL> whenever sqlerror exit FAILURE
</b>

 

Thanks a lot!

A reader, December 10, 2004 - 8:33 pm UTC




Getting UNIX environment variable in PL/SQL

Debajit Mishra, June 08, 2005 - 7:14 pm UTC

Hi Tom,
I always like knowing new stuff from this site.

Unix variables can be used in PL/SQL as follows,
Example : t1.ksh
##
numeric_parameter=100
$ORACLE_HOME/bin/sqlplus /nolog << EOF
  connect $SCRIPT_ORA_USER/$DW$ORACLE_DB;
  set serveroutput on
  spool aa.txt
  declare
    v_oracle_home VARCHAR2(100) := '$ORACLE_HOME';
    v_numeric_parameter number := $numeric_parameter;
  begin
    dbms_output.put_line('oracle_home->'||v_oracle_home);
    dbms_output.put_line('numeric_parameter->'||v_numeric_parameter);
  end;
/
  spool off
exit
EOF
##

When executed : ksh t1.ksh 

----The output of this script will be similar to 
----the following :
oracle_home->/oracle/app/oracle/product/9.2.0
numeric_parameter->100

SQL>   spool off
----

Thanks.

Debjit

 

Tom Kyte
June 08, 2005 - 10:21 pm UTC

but that is NOT using an environment variable in plsql.

that is demonstrating how to write a script, that generates a plsql block, that is submitted to sqlplus.


plsql is not accessing an environment variable.

a shell script is writing a script that has an environment variable value placed into it.


If plsql could access an environment variable, you'd be able to write a stored procedure that access one - but you cannot.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library