Solved

Passing variables to sql in a unix shell script

Posted on 2004-03-23
6
4,940 Views
Last Modified: 2013-12-12
Hi

Hopefully this is the correct area for this question- was not sure if it should come under UNIX or Oracle.

Been struggling all morning with this and wondered if anyone could help.  Basically I'm creating a variable from a sql statement and then passing this variable into another sql statement as a parameter and it does not work.  If I create the variable manually i.e EOD_TYPE="'EOD'" and then pass the parameter to the 2nd sql statement as ${EOD_TYPE} it works, if I use a select to create the variable i get the following error:
SP2-0042: unknown command "'EOD'" - rest of line ignored.

It's quite a short script so I'll paste it below for reference.

thanks in advance

Paul


#!/bin/ksh

#EOD_TYPE="'EOD'"

EOD_TYPE=`sqlplus -s /   <<EOF
set head off
set feedback off
set pages
select decode(to_char(bus_date,'Day'),'Friday','''EOW''','''EOD''') from trading_area where trga_code='EUR';
EOF`

EOD_RUN_TIME=`sqlplus -s /   <<EOF
set head off
set feedback off
set pages
select proc_time_min*60 from eod_run_time where eod_type=${EOD_TYPE};
EOF`

echo $EOD_RUN_TIME
0
Comment
Question by:pjd1
[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
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:mudumbe
ID: 10659007
Try without the curly braces:

select proc_time_min*60 from eod_run_time where eod_type=$EOD_TYPE;
0
 

Author Comment

by:pjd1
ID: 10659121
sorry - still does not work - get the same error.

Paul
0
 
LVL 5

Expert Comment

by:mudumbe
ID: 10659215
Which sqlplus is causing error?  If the first one works correctly does it set the value of EOD_TYPE correctly?  Include echo $EOD_TYPE after the first sqlplus in your script.
0
Independent Software Vendors: 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!

 

Author Comment

by:pjd1
ID: 10659342
both sql's work OK - it's the 2nd one that fails if the variable is created via the 1st sql.  If I put the echo in I get 'EOD' which is what I would expect.
0
 
LVL 5

Accepted Solution

by:
gmyers earned 125 total points
ID: 10663318
I've a feeling that you've got some extra characters at the start of EOD_TYPE
so that it is effectively executing :

select proc_time_min*60 from eod_run_time where eod_type=

'EOD';

Maybe something in a glogin.sql script ?

You can try something like

sqlplus -s / <<EOF
set serveroutput on size 100000
declare
  v_char varchar2(2000);
begin
  select substr(dump('${EOD_TYPE}'),1,250) into v_char from dual;
  dbms_output.put_line(v_char);
end;
.
/
EOF

to see what SQL is getting in that variable

Ideally you want something like
Typ=96 Len=3: 69,79,68
(ie a three character value with just ASCII characters)
0
 

Author Comment

by:pjd1
ID: 10665783
Great that worked - there were 3 spaces at the begining.  I've used tr -d to remove the spaces and all is now well.

thanks
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Procedure syntax 5 63
format dd/mm/yyyy parameter 16 58
error doing substr 3 51
Deleting multiple child tables from parent in one SQL statement 20 55
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

739 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