How to get Oracle variable in UNIX?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get Oracle variable in UNIX?
# 1  
Old 02-27-2013
How to get Oracle variable in UNIX?

Hello All,

I need to export various variables in UNIX. But the values of those variables will come from Oracle.

Say i have Oracle query:
Code:
 
Select
file_id, file_desc, file_freq_cd, load_table_nm, load_stored_procd_nm, load_proc_typ_nm, err_rec_cnt_thrsld_nbr, file_expc_rec_cnt From Table1 where file_id=1

The above query will always return one row.

i have variables to be exported for each of the columns selected above as below:

Code:
 
export FILE_ID=
export FILE_DESC=
export FILE_FREQ_CD_ID=
export LOAD_TBL_NM=
export LOAD_STORED_PROCD_NM=
export LOAD_PROCD_TYP_NM=
export LOWER_SHORT_NM=
export ERR_REC_CNT_THRSLD_NBR=
export FILE_EXPC_REC_CNT=


I am not sure how can i do this at once. Kindly help me on the same
Any help appreciated!


---------- Post updated at 01:52 PM ---------- Previous update was at 12:58 PM ----------

Can anybody please help on the above issue ?
# 2  
Old 02-27-2013
Its not easy to understand what you like.

If output is like this: 34,23,645,82,110,412,8,65 you can try this:

Code:
echo "34,23,645,82,110,412,8,65" | awk -F, '
{ print "export FILE_ID="$1}
{ print "export FILE_DESC="$2}
{ print "export FILE_FREQ_CD_ID="$3}
{ print "export LOAD_TBL_NM="$4}
{ print "export LOAD_STORED_PROCD_NM="$5}
{ print "export LOAD_PROCD_TYP_NM="$6}
{ print "export LOWER_SHORT_NM="}
{ print "export ERR_REC_CNT_THRSLD_NBR="$7}
{ print "export FILE_EXPC_REC_CNT="$8}'


export FILE_ID=34
export FILE_DESC=23
export FILE_FREQ_CD_ID=645
export LOAD_TBL_NM=82
export LOAD_STORED_PROCD_NM=110
export LOAD_PROCD_TYP_NM=412
export LOWER_SHORT_NM=
export ERR_REC_CNT_THRSLD_NBR=8
export FILE_EXPC_REC_CNT=65

Your SQL example do miss input for LOWER_SHORT_NM
# 3  
Old 02-27-2013
The sqlplus (part of the oracle distribution) command returns 3 lines, the capitalised field names , a separator line and the values.
Code:
$(echo $(sqlplus -s /nolog <<EOQ
connect ${USER}/${PASS}@${SID}
set linesize 1000
select
  file_id, 
  file_desc, 
  file_freq_cd, 
  load_table_nm, 
  load_stored_procd_nm, 
  load_proc_typ_nm, 
  err_rec_cnt_thrsld_nbr, 
  file_expc_rec_cnt 
From Table1 
where file_id=1 ;
quit
EOQ )
| grep -v '-----'|awk 'if(/^FILE_ID/){print "names=("$0")"}else{print "values=("$0")";})
index=0
for i in ${names[@]} ; do
  export $i=${values[$index]}
  index=$(($index + 1 ))
done

So the next step is to read the field names and values into 2 arrays and then step through the arrays assigning and exporting the values

I use grep to remove the unwanted separator line, then use awk to write out my array assignment statements, these are evaluated by the $(...) expansion.

Then we simply step through the 2 arrays...

WARNING: I have not tested this code, some errors will have crept in, but you get the idea...
# 4  
Old 02-27-2013
You can turn off heading and seperator and skip the need to process them.
The code is tested using a different query to give an idea how this could be done:
Code:
#!/bin/bash
read READ_UN READ_SD <<< $(sqlplus -s $USERNAME/$PASSWORD <<SQL
set heading off
select user, sysdate from dual;
SQL
)
echo $READ_UN
echo $READ_SD

# 5  
Old 02-27-2013
read thing doesnt work cero as i do have spaces in the values itself as in one column has a value arun mishra.. read will treat it as two fields instead of 1.
can i assign evrything to arrayvariable and fetch 1 by 1:

Code:
 
arrayvar=`sqlplus -s ${WMD_DM_CONNECT} <<-EOF
set wrap off
set linesize 30000
set feedback off
set pagesize 0
set verify off
select file_id, file_desc, file_freq_cd, load_tbl_nm, load_stored_procd_nm, load_proc_typ_nm, err_rec_cnt_thrsld_nbr, file_expc_rec_cnt,grth_pct,rec_trminator_txt
from wmd_file_lkup where file_shrt_nm='CGENSITE';
exit
EOF`
 
export FIA_ID=aarrayvar[0]
export FIA_DESC=aarrayvar[1]..
 
....
.....
and so on..

let me know if i make some sense.. and kindly suggest on the same.
# 6  
Old 02-27-2013
I don't think the array approach will solve the problem with spaces.
An ugly solution that comes to my mind is to generate the export statements via sql and execute them. Enclose the fields with spaces with double quotes.
Code:
#!/bin/bash
eval $(sqlplus -s $USERNAME/$PASSWORD<<SQL
set heading off
set lines 10000
select 'export EXP_SD="'||to_char(sysdate,'YYYY MM DD HH24 MI')||'"; export EXP_UN='||user from dual;
SQL
)
echo "$EXP_SD"
echo "$EXP_UN"

Edit: this may fail if you generate very long lines, but you can query only a few fields in one go and have consecutive queries do the rest.

Last edited by cero; 02-27-2013 at 07:54 AM..
# 7  
Old 02-27-2013
I have a faint memory from over ten years back that you can set the column separator in SQL. Do that, and set the shell's IFS variable accordingly, and the read thing should succeed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable from file to Oracle

cat a1 scott robert tom test script : #!/usr/bin/ksh for NAME in `cat a1` do VALUE=`sqlplus -silent "nobody/bobody01@testq" <<END set pagesize 0 feedback off verify off heading off echo off select username from dba_users where username=upper('$NAME'); END` if ; then echo... (3 Replies)
Discussion started by: jhonnyrip
3 Replies

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

3. Shell Programming and Scripting

How to get a numeric value from Oracle to UNIX variable without spaces?

Hi, I am using the below code to get a numeric value from oracle to unix variable: BD_RC_CNT=`sqlplus -s ${WMD_DM_CONNECT} <<EOF set heading off set pagesize 0 Select count(*) from wmd_bad_data where proc_id = ${PROC_ID} and file_id = ${FILE_ID} and file_dt =... (7 Replies)
Discussion started by: Arun Mishra
7 Replies

4. Programming

Oracle Variable Passing Test

Hi, I am trying to get the oracle variables and pass the values in sql placed in procedure. VARIABLE vstat='ASDS,FGDS,VCGD,VCXC' Query : select distinct dept from College where section in ('C','D') AND CODES ='' AND NAMES IN ('RAJ','SAM'); I want CODES values to be taken from vstat... (1 Reply)
Discussion started by: Perlbaby
1 Replies

5. Shell Programming and Scripting

set oracle variable in function

Hi, I have a function that is suposed to generate a AWR report: #-----------------------# gen_awr() #-----------------------# { sqlplus -s admin/admin@OCEAN11<<ENDOFSQL define num_days = '' define report_type = "html" define begin_snap =$snap1 define end_snap =%snap2 define... (1 Reply)
Discussion started by: amitlib
1 Replies

6. Shell Programming and Scripting

Passing unix variable to oracle parameters

Please help me how to pass some unix vairable to oracle. I have used below , but not displaying passed (inval) value. calling() { sqlplus -s $1/$2@$3 <<EOF begin exec call_sql($4); end; exit EOF } calling user pwd inst value1... (17 Replies)
Discussion started by: Jairaj
17 Replies

7. Shell Programming and Scripting

passing variable to oracle procedure

using the script below I want to pass a parameters thorugh my sql call(@/unixsxxx/xxxx/helpenv.sql emptab ) as input into an oracle procedure xxxx_package.proc1(%1,emptab); . I tried %1 but it does not work. Any suggestions. #!bin/ksh set -x # export... (0 Replies)
Discussion started by: TimHortons
0 Replies

8. Shell Programming and Scripting

To get value from oracle & assign it in UNIX variable

Hi Team, I need to get data from oracle table & need to assign that value to unix variable. I have serched the same in other threads. I found the following code. I have tried code to get the value from oracle. but it is not working. The error shows invalid identifier "NAM" & then list all... (5 Replies)
Discussion started by: Amit.Sagpariya
5 Replies

9. UNIX for Dummies Questions & Answers

How to pass unix variable to oracle

hi , how to pass unix variable to oracle code is ............. #! /bin/ksh echo enter date vale read date1 sqlplus x/y@oracle select * from emp where statrt_date= $date1 is this is correct way... (1 Reply)
Discussion started by: chiru
1 Replies

10. UNIX for Dummies Questions & Answers

Get Oracle fuction return value in a variable

Hi All, :confused: I have the following code. var=' ' sqlplus user/pass@DB <<EOF whenever sqlerror exit 1 select package.func() into $var from dual; EOF echo $var But, this code does not work to display the value returned by the oracle function. Do we have to bind variables before... (3 Replies)
Discussion started by: rahulrathod
3 Replies
Login or Register to Ask a Question