Solved

pass pl/sql variable to unix

Posted on 2003-11-21
10
6,367 Views
Last Modified: 2007-12-19
ok, here's what i need to do.  i have a unix shell script (hp unix, ksh shell) that is started each night via the crontab.  i need the shell script to start, set shell variable, log into a database, check an oracle 8.1.7 table for a flag value. pass the value of this flag back to the unix shell script variable.

start shell script.
set unix variable.
log into oracle 8.1.7 (on same box).
check table for flag.
send flag value out to unix
if unix variable is x, do y
if unix variable is a, do b

so, is this possible?  can you send the value of a pl/sql variable out to unix? please help.
0
Comment
Question by:benpung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 48

Expert Comment

by:schwertner
ID: 9796985
You can use UTL_FILE package of PL/SQL to create a OS file and to put everything you need there.
Unix has excelellent tools for reading flat files.

An example:

procedure YOUR_PROC   is

   sasha SYS.UTL_FILE.FILE_TYPE;

    sanjeev   VARCHAR2(1800);

begin

  sasha := UTL_File.Fopen('D:\staff\cv','56789.txt', 'r');
 

LOOP

UTL_File.get_line(sasha,sanjeev);

-- FORMAT THE DATA in/from buffer sanjeev and insert in Oracle DB using INSERT statement

END LOOP              
               
EXCEPTION
WHEN no_data_found THEN  
UTL_File.Fclose(sasha);                      
WHEN  UTL_File.invalid_filehandle   THEN ... UTL_File.Fclose(sasha);  
      --   invalid_filehandle - not a valid file handle
WHEN  UTL_File.read_error   THEN ... UTL_File.Fclose(sasha);
       -- OS error occurred during read
WHEN  UTL_File.invalid_operation   THEN ... UTL_File.Fclose(sasha);                    

end  YOUR_PROC ;


REMARK
Server security for PL/SQL file I/O consists of a restriction on the directories that can be accessed.
Accessible directories must be specified in the
instance parameter initialization file (INIT.ORA).

Specify the accessible directories for the UTL_FILE
functions in the initialization file
using the UTL_FILE_DIR parameter.
For example:
UTL_FILE_DIR = <directory name>
like in
UTL_FILE_DIR = C:\DATA\ORACLE\DATAFILES
0
 
LVL 6

Expert Comment

by:morphman
ID: 9798470
Or if like some (me included), you dont have access to the utl_file_dir, or to change it, you can do the following.

In your unix script do the following

VAR=`sqlplus -s user/password @script`

In your plsql script, put the following sqlplus commands at the top.

------------------------------
set echo off
set feedback off
set serveroutput on

begin

dbms_output.put_line(plsql_var);
end;

---------------------------------
0
 
LVL 1

Author Comment

by:benpung
ID: 9805785
thanks for the posts. i'll work with these ideas some when i get in the office tomorrow. please check back!
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 1

Author Comment

by:benpung
ID: 9809805
ok, i've tried morphman's suggestion above.  i've posted my two scripts below. when i run the .ksh unix shell script, it just hangs. it logs in to the database and just sits there. can someone tell me what i'm doing wrong?

.ksh shell script***********************8
# . /oracle/local/scripts/oracle.kshrc needs to be ran in order
# for the SQL code to be understood.
. /oracle/local/scripts/oracle.kshrc

# Environment variables
DBINSTANCE=mydatabase
LOGON_NAME=mylogon
LOGON_PASSWD=mypswd
SQL_DIR="/home/ehsitdev/EIMS/sql"

# Change directory to the xml directory.  This directory holds the sql script.
cd $SQL_DIR

# Connect to Oracle database
inst $DBINSTANCE

# Start sqlplus, log in to database, and initiate error log file
B_COMPLETE_VAR=`sqlplus -s $LOGON_NAME/$LOGON_PASSWD @rover_complete.sql`
--it seems to be hanging here.
EXIT
echo B_COMPLETE_VAR

sql script *********************************
SET ECHO OFF;
SET FEEDBACK OFF;
SET SERVEROUTPUT ON;
DECLARE
   c_holder     VARCHAR2 (5);
BEGIN
   SELECT reqcompflag
     INTO c_holder
     FROM ems_owner.report_requests
    WHERE reqcompflag = 'N';

   IF c_holder = 'N'
   THEN
      DBMS_OUTPUT.put_line ('N');
   END IF;
EXCEPTION
   WHEN NO_DATA_FOUND
   THEN
      DBMS_OUTPUT.put_line ('Y');
   WHEN TOO_MANY_ROWS
   THEN
      DBMS_OUTPUT.put_line ('N');
END;
/
0
 
LVL 6

Expert Comment

by:morphman
ID: 9811487
you probably need to put an

exit

at the end of the sql script.

0
 
LVL 1

Author Comment

by:benpung
ID: 9813590
ok, that seems to work. now when i do it though, the value of my unix variable is:

Y Input truncated to 5 characters

do you know which setting i have to change in oracle so i don't get the "Input truncated to 5 characters" after my value?
0
 
LVL 6

Accepted Solution

by:
morphman earned 125 total points
ID: 9813867
Input truncated to 2 characters ... error
You may get this message if you use an editor that allows you to save and exit without entering an end-of-line character. For example, if your last line contains just one character '/', you save and exit without pressing the return key to end the line, you may get the error: Input truncated to 1 characters.

You can usually remove this error by making sure that all your lines are properly terminated.
 Try that..
0
 
LVL 6

Expert Comment

by:morphman
ID: 9813898
alternativeley

dbms_output.put_line('###'||var||'###');


and get it from your script like this

var=`sqlplus log/pass @script.sql`

var2=`echo ${var} | cut -d# -f3`

We use this to prevent losing spaces and other characters.
0
 
LVL 6

Expert Comment

by:morphman
ID: 9813903
sorry should be

var2=`echo ${var} | cut -d# -f4`

0
 
LVL 1

Author Comment

by:benpung
ID: 9819020
it's working great now. just what i needed. thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Email query results in HTML 6 50
Fill Date time Field 12 45
Error in creating a view. 8 52
tablespace - increase the size 2 16
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question