Oracle Query results to be stored in variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Oracle Query results to be stored in variables
# 1  
Old 03-22-2008
Oracle Query results to be stored in variables

Hi

I would like to know if there is a way to just have one ORACLE connection established, using which we can execute different queries and store the results under different variables.

For e.g the following uses to two silent ORACLE connection to store the result under two different variables. Instead can we just be able to modify the code to use the same connection against multiple queries?

#!/bin/bash
Query1=`sqlplus -s user/pwd@host<<eof
set serveroutput on;
set feedback off;
set linesize 1000;
select name from success; --Just for e.g.
EXIT;
eof`
....Operation on $Query1

Query2=`sqlplus -s user/pwd@host<<eof
set serveroutput on;
set feedback off;
set linesize 1000;
select name from failure; --Just for e.g.
EXIT;
eof`
....Operation on $Query2

The reason for asking is that the same code is required to be repeated multiple times.

Also, can you pls explain how to read the results stored in the variable line by line.

For e.g.

if Query1 results are:

int1
int4
int5

I would like to be able to get int1 value first, get the second one int4 and get the last one int5. Just would like to know how to perform a loop and read each line.

Thanks for your help.
Ashok
# 2  
Old 03-24-2008
Print the Query output in Shell Script.

RETVAL=`sqlplus -s user/pwd@host <<EOF
SET SERVEROUTPUT ON SIZE 100000
Declare

OUT_MSG VARCHAR2(200);

Begin

select name into OUT_MSG from table_success;

dbms_output.put_line ('KeepThis '||nvl(OUT_MSG,''));
End;
/
SET SERVEROUTPUT OFF
EXIT;
EOF`

X=`echo $RETVAL | grep KeepThis | awk '{print $2}'`
Y=`echo $RETVAL | grep KeepThis | awk '{print $3}'`
Z=`echo $RETVAL | grep KeepThis | awk '{print $4}'`


echo "The Query output is: "

echo "Query OUT_MSG 1= $X "
echo "Query OUT_MSG 2= $Y "
echo "Query OUT_MSG 3= $Z "

If you have more outputs from query, print those results in $5, $6,......
# 3  
Old 03-13-2009
Need to change the answer.

Hi,

This answer does not work in SQL 8i if the SQL query returns more values. Is there any other way to store multiple values in a variable?

Thanks.
# 4  
Old 03-14-2009
I hope this will help you

Hi ,

First redirect the query result to a file named "query_result.txt "
as shown below


#!/bin/ksh

sqlplus -s $USERID/$USERPWD <<EOF >query_result.txt
set heading off feedback off pagesize 0 linesize 30000 trimout on ;
select data from table_name ;
exit;
EOF


####after redirecting the output to a file fetch the value line by line as shown in below


for i in `cat query_result.txt `
do

echo $i

##do what ever u want to


delete from table_name where column_name = $i ;

##note : refer the column value as $i to get the value


done



- - - > Explaination :



here the refer the value to i ,this will fetch the records line by line
for eg : if the query output is

red
blue
green


here for the
1st iteration : i value := red
2nd iteration : i value := blue
and so on ...

I hope this wil help you ..
# 5  
Old 03-16-2009
You can set a shell array to the SQL results as such:
Code:
$cat test.ksh
#!/bin/ksh
set -A SQL_RESULTS_ARRAY $(
sqlplus -S <<EOF
user/passwd
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF ECHO OFF SERVEROUT ON TIME OFF TIMING OFF
SELECT TO_CHAR(SYSDATE, 'MM/DD/YYYY'),
       TO_CHAR(SYSDATE - 1, 'MM/DD/YYYY')
  FROM DUAL;
EOF
)

SQL_ARRAYCOUNT=${#SQL_RESULTS_ARRAY[*]}
SQL_ARRAYIDX=0

while (( $SQL_ARRAYIDX < $SQL_ARRAYCOUNT ))
do
 print "SQL_ARRAY[$SQL_ARRAYIDX]=(${SQL_RESULTS_ARRAY[$SQL_ARRAYIDX]})"
 SQL_ARRAYIDX=$(($SQL_ARRAYIDX+1))
done

exit 0
$./test.ksh
SQL_ARRAY[0]=(03/16/2009)
SQL_ARRAY[1]=(03/15/2009)
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning multiple column's value from Oracle query to multiple variables in UNIX

Hi All, I need to read values of 10 columns from oracle query and assign the same to 10 unix variables. The query will return only one record(row). I tried to append all these columns using a delimiter(;) in the select query and assign the same to a single variable(V) in unix. I thought I... (3 Replies)
Discussion started by: hkrishnan91
3 Replies

2. Shell Programming and Scripting

How to Assign SQL Query Results to Variables in Linux?

Hi, I am new to linux... How to Assign SQL Query Results to Variables in Linux,i want ti generate it in param files, Can anyone please explain me. Ex: SQL> Select * from EMP; O/P: Emp_No Emp_Name 1 AAA 2 BBB 3 CCC and I want expected... (5 Replies)
Discussion started by: Sravana Kumar
5 Replies

3. Shell Programming and Scripting

Multiple Query Results to Variables

Hello, I am very new to shell scripting and I am not sure of how best to handle the following scenario. I need to query a list of values from a table. I need to store those results and use them to selectively delete values in yet another table in a separate database. I do know how to store the... (3 Replies)
Discussion started by: flowervz
3 Replies

4. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

5. Shell Programming and Scripting

SQL/Plus in a coprocess example. Also saves query results into shell variables

While assisting a forum member, I recommended running SQL/Plus in a coprocess (to make database connections and run a test script) for the duration of his script rather than starting/stopping it once for every row in a file he was processing. I recalled I made a coprocess example for folks at... (2 Replies)
Discussion started by: gary_w
2 Replies

6. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

7. Shell Programming and Scripting

Oracle Query results to be stored in variables using unix

I want to store the sql query output into a variable #!/bin/ksh ORACLE_SID=DB01; export ORACLE_SID; export FILE_PATH=/home/asg/Tmp # Order Checking echo " removing old files " rm $FILE_PATH/Malformed_Order.txt echo " Enter the Malformed Order ....!" read orders echo "Regrade... (5 Replies)
Discussion started by: Nareshp
5 Replies

8. UNIX for Advanced & Expert Users

Set shell variables from SQLPLUS query results

Hi All, I needed to get the result of two sqlplus queris into shell variables. After days of looking for the ultimate solution to this problem.. i found this... sqlplus -s USER/PASS@DB <<EOF | awk '{if(NR==1) printf("%s ", $1); if(NR==2) printf("%s ", $1);}' | read VAR1 VAR2 set head off... (2 Replies)
Discussion started by: pranavagarwal
2 Replies

9. UNIX for Advanced & Expert Users

Environmental Variables - where stored ?

Hi all ! Yesterday I defined an environmental variable PATH, but today when I restarted machine, I could not see that it was stored any place. Is there any file where I could save the settings ? I have quite a few env.variables defined, so I need a smarter way to define. regards D (5 Replies)
Discussion started by: DGoubine
5 Replies

10. UNIX for Advanced & Expert Users

Oracle stored procedure.

I am using sqlplus. I have the stored procedure name. How can i print the stored procedure content? (2 Replies)
Discussion started by: kamil
2 Replies
Login or Register to Ask a Question