I want to get the Unix variable value in the sql stmt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I want to get the Unix variable value in the sql stmt
# 1  
Old 08-27-2010
I want to get the Unix variable value in the sql stmt

Hi All
I have a requirement where in I am stuck. There is a shell script that is being developed by me. It consist of the sql stmt also. I need to export a variable called HOMEPAGE with a value say www.abc.com. and then use this $HOMEPAGE variable in the sql stmt. My ultimate aim is to fetch all the values from the sql stmt and print it in a file.
Look for the text "### here is the place where in I want the $HOMEPAGE value."
Here is the program:
Code:
### Set the global variables.
CONNECT=$1
LOG=$MMHOME/log/ang_stdnld_`date +'%b_%d'`.log
ERR=$MMHOME/error/err.ang_stdnld.`date +'%b_%d'`
##SCRIPT_HOME=$MMHOME/oracle/proc/bin
### Check if path where extracts are to be created is passed as arg.
### If this arg is Null, then default to PWD.
if [ "$2" = "" ]
then
   DIR=`pwd`
   echo "Path is not sent. Defaulting to PWD: "`pwd`
else
   if [ -d $2 ]
   then
      DIR=$2
   else
      echo "Path $2 does not exist"
      exit 1
   fi
fi
#-------------------------------------------------------------------------
# Function Name: remove_files()
# Purpose      : Clean the temporary files created.
#-------------------------------------------------------------------------`
remove_files()
{
  echo "Removing temporary files...\n" >> $LOG
  # Clean up temp list files.
  rm -f $CHAIN
  if [[ -s $ERR ]]
  then
    :
  else
     rm -f $ERR
  fi
  return 0
}
### end remove_files
   echo "Getting the chain name...\n" >>$LOG
   # Set filename to contain this chain 
   CHAIN=$DIR/stdnld_chain.lst 
   $ORACLE_HOME/bin/sqlplus -s $CONNECT <<EOF >>$CHAIN
   set pause off
   set echo off
   set heading off
   set feedback off
   set verify off
   set pages 0
   select distinct sh.chain
     from store st,
          store_hierarchy sh,
          period p
    where st.store = sh.store
      and p.vdate between st.store_open_date and nvl(st.store_close_date,p.vdate)
    order by chain;
EOF
   if [ `grep "^ORA-" $CHAIN | wc -l` -gt 0 ]
   then
      cat $CHAIN >> $ERR
      echo "ORA Error while creating Chain listing in ang_get_chain()." >> $LOG
      exit 1
   fi
   for i in `cat $CHAIN`
   do
     returnedchainvalue=`$ORACLE_HOME/bin/sqlplus -s $CONNECT <<EOF
     set serveroutput on;
     set pause off
     set echo off
     set heading off
     set feedback off
     set verify off
     set pages 0
     Declare
        message VARCHAR2(200);
     Begin
        select ANGELIC_PKG.ANG_GET_CHAIN_NAME($i) into message from dual;
        dbms_output.put_line (message);
     End;
     /
     set serveroutput off;
     EXIT;
EOF`     
 X=`echo $returnedchainvalue`     
$ORACLE_HOME/bin/sqlplus -s $CONNECT <<EOF >>${X}_en-us_stores_001
   set heading off
   set feedback off
   set pages 0 
   set linesize 32000
   set trimspool on
   set trimout on
SELECT 'retailstoreid'||
       chr(9)||
       'name'||
       chr(9)||
       'main phone'||
       chr(9)||
       substr('address line 1',1,80)||
       chr(9)||
       substr('address line 2',1,80)||
       chr(9)||
       substr('city',1,80)||
       chr(9)||
       substr('state',1,80)||
       chr(9)||
       'postal code'||
       chr(9)||
       'country code'||
       chr(9)||
       'home page'||
       chr(9)||
       'hours'||
       chr(9)||
       'category'||
       chr(9)||
       'description'||
       chr(9)||
       'currency'||
       chr(9)||
       'established date'||
       chr(9)||
       'latitude'||
       chr(9)||
       'longitude'
FROM DUAL;
SELECT S.STORE||
       chr(9)||
       S.STORE_NAME||
       chr(9)||
       NVL(S.PHONE_NUMBER, 'xxx-xxx-xxxx')||
       chr(9)||
       substr(A.ADD_1,1,80)||
       chr(9)||
       substr(A.ADD_2,1,80)||
       chr(9)||
       substr(A.CITY,1,80)||
       chr(9)||
       substr(NVL(A.STATE,'xx'),1,80)||
       chr(9)||
       NVL(A.POST, 'xxxxx')||
       chr(9)||
       A.COUNTRY_ID||
       chr(9)||
       ''|| ### here is the place where in I want the $HOMEPAGE value.
       chr(9)||
       ''||
       chr(9)||
       ''||
       chr(9)||
       ''||
       chr(9)||
       S.CURRENCY_CODE||
       chr(9)||
       to_char(S.STORE_OPEN_DATE, 'MM/DD/YYYY')||
       chr(9)||
       ''||
       chr(9)||
       ''
FROM STORE S,
     ADDR A,
     STORE_HIERARCHY SH,
     PERIOD P
WHERE A.MODULE='ST'
  AND A.COUNTRY_ID='US'
  AND A.KEY_VALUE_1=S.STORE
  AND A.ADDR_TYPE=01
  AND S.STORE=SH.STORE
  AND S.STORE_OPEN_DATE <= P.VDATE
  AND S.STORE_CLOSE_DATE >= P.VDATE
  AND SH.CHAIN=$i;
EOF
done
remove_files
### end ang_stdnld


Last edited by Scott; 08-27-2010 at 03:19 AM.. Reason: Code tags, please...
# 2  
Old 08-27-2010
Sorry, but I didn't understand your problem, but I can suggest you to keep SQL queries out of shell scripts, it keeps the code clear.

The other way to do it is to place the queries in files separated from the shell script and use Oracle SQLPlus substitution variables.

Check this link: SQL*Plus FAQ - Oracle FAQ

For example:
Code:
cat myQuery.sql
SET HEAD OFF
SET FEEDBACK OFF
SET VERIFY OFF
SELECT TO_DATE('&&1', 'YYYYMMDD') TEST FROM DUAL;
EXIT

And execute like below:
Code:
echo "START myQuery.sql 20100827" | sqlplus -S -L <user>/<pass>@${ORACLE_SID}

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign the return value of the SQL to a variable in UNIX

Hi I am new to UNIX. I am trying the below and getting the error. I am trying to assign the variable with the value of the query result. I want this value to use in the next steps. Created UNIX file (Batch.sh) as below #!/bin/ksh sqlplus callidus/callidus4u@attstcal @Batch.sql ... (2 Replies)
Discussion started by: KrishBalu
2 Replies

2. Shell Programming and Scripting

SQL output to UNIX variable

I have a sql statement , i need to assign to a variable in Unix sel count(*) AS num_files from TABLE_A; i need to use "num_files" in unix statements. let me know how to assign unix variable to above num_files (1 Reply)
Discussion started by: nani1984
1 Replies

3. Shell Programming and Scripting

UNIX variable to SQL statement

The following is my script : #!/bin/bash echo "please give app_instance_id" read app_instance_id echo "id is $app_instance_id" export app_id=app_instance_id sqlplus -s nnviewer/lookup@//nasolora008.enterprisenet.org:1521/LOAD3 @test.sql<<EOF SPOOL /home/tibco/MCH/Data/qa/raak/name.xls... (4 Replies)
Discussion started by: raakeshr
4 Replies

4. Shell Programming and Scripting

How to use GOTO stmt in Unix scripting?

my code does somthing like this: #!bin/ksh sqlplus / | While read id do temp=`echo $id` i = i+1 done j=0 while do --connecting to sql and executing a Stored proc for 1st id --checking for the status status = $? if error --need to... (1 Reply)
Discussion started by: RP09
1 Replies

5. Shell Programming and Scripting

Passing a string variable from Unix to Sql Plus

Hi Guys, I am trying to pass a string variable from Unix shell script to sqlplus as a parameter. I have tried using single quotes with the variable name but it does not work. Please help me with it. I am using BASH. My code: Your help is much appreciated. Thanks, shil (2 Replies)
Discussion started by: infintenumbers
2 Replies

6. Shell Programming and Scripting

Help with Dates and SQl stmt?

hi All Please explain the below statement in RED?What does this mean? perl /HDS/common/operations/Quality_Team/Nirvana/WEEKLY_OOPS/sql_dump.pl --sql "select * from WEEKLY_REPORT order by test_case, type" --username=ddb_qa --password=ddb_qa123 --sid=pldeldb --output... (1 Reply)
Discussion started by: SVS_2017
1 Replies

7. Shell Programming and Scripting

convert file into sql insert stmt

My file is now cleaned, sanitized & prepped: 07/07/2008 21:18:51 Installation 52016 complete *BUT NOTHING CHANGED* 07/21/2008 15:28:15 Removal 52016 complete 07/21/2008 15:34:15 Removal 55856 complete 12/08/2009 19:30:40 Installation 62323 complete 12/08/2009 19:39:06 Installation ... (6 Replies)
Discussion started by: dba_frog
6 Replies

8. Shell Programming and Scripting

Using Unix Variable in a PL/SQL block

Hi, I have a unix varaible called as account which hold values which i want to use in a PL/SQL block in a shell script. This variable value is being used in multiple places in the PL/SQL block and i get an erroe whenenevr is use this varaible with $prompt. Help Urgently (1 Reply)
Discussion started by: sumi_mn
1 Replies

9. Shell Programming and Scripting

sql query variable not exactly unix

I know htis isnt exactly unix.... but hopefully someone can help me or direct me someplace to get help. I can run sql queries in scripts against my informix db using: dbaccess mydb myquery.sql >> sql.output I need to write my script to select based on todays date. Its very... (5 Replies)
Discussion started by: MizzGail
5 Replies
Login or Register to Ask a Question