How to store the data retrived by a select query into variables?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to store the data retrived by a select query into variables?
# 1  
Old 01-17-2008
CPU & Memory How to store the data retrived by a select query into variables?

Hi Friends,

Can u please help mw with the following query .

I need to run a database sql statement to select particular fields from a table.I need to store the retrieved filed values into variables so that i can print it in a specific format. But the particular select query retrieves more than 1 row .I have redirected the result of the query to a temporary text file.Lets say there are 10 rows retrieved. These retrieved values(filed_name value) are written into say 1.txt.
Now 1.txt looks some what like this :

fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value fld_name value

I thought of using a grep command to get the values and store it in a variable.But the format of this text file makes it impossible.

Can please help me to put the data as below in 1.txt

fld_name value
fld_name value
fld_name value
fld_name value
fld_name value
fld_name value

or
When we run a select statement the rows are retrieved in the below format :
fld_name value
fld_name value
fld_name value

fld_name value
fld_name value
fld_name value


fld_name value
fld_name value
fld_name value


3 rows retrieved

Is there any way to store exactly this way to 1.txt?

Thanks in advance
Jisha
# 2  
Old 01-17-2008
use spool

You can write the following statement on the SQL prompt, before the Select query


Quote:
SQL>spool 1.txt;
SQL> select ....
SQL> spool off;
# 3  
Old 01-17-2008
Quote:
Originally Posted by gauravgoel
You can write the following statement on the SQL prompt, before the Select query
Thank you gauravgoel. .
My script is like this :

`isqlrf <database_name> - <<EOFSQL
select ....
from .....
where .....;
EOFSQl`

How i put the output of the above query was as below:

values=`isqlrf <database_name> - <<EOFSQL
select ....
from .....
where .....;
EOFSQl`

echo $values > 1.txt

I dont know anything about spools.Do u think my way of approach is correct?

Many Thanks
Jisha
# 4  
Old 01-17-2008
as I mentioned before your select put

spool 1.txt

and after select put

spool off;


and after this you neednot redirect the output to 1.txt
# 5  
Old 01-17-2008
I did like this :
`isqlrf <database_name> - spool 1.txt;<<EOFSQL
select ....
from .....
where .....;
spool off;
EOFSQl`

I executed & nothing happened .The file was also not created.

Thanks
Jisha
# 6  
Old 01-17-2008
try like this

`isqlrf <database_name>;<<EOFSQL
spool 1.txt
select ....
from .....
where .....;
spool off;
EOFSQl`


you may need to tweak it around a bit,
Sorry cant check right now for you as dont have access to UNIX system right now
# 7  
Old 01-17-2008
Thanks again.
I tried it like this:
`isqlrf <database_name> <<EOFSQL
spool 1.txt

select ....
from .....
where .....;
spool off;
EOFSQl`

It gave me a "syntax error has occured"

Then i tried it :
`isqlrf <database_name>;<<EOFSQL
spool 1.txt

select ....
from .....
where .....;
spool off;
EOFSQl`

This didnt give me any answer at all ..The command prompt was also not there after execution.I had to pred ctrl+c to go to the command prompt..Do u know y this happens?

Thanks,
jisha
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

need to store query output fields in variables edit them and update the same in tables.

Hi , I have a query like select err_qty,drop_qty,unbld_qty,orig_qty from usage_data; I need to store the values of these fetched fields in variables, Need to edit them and update the new values into the table. Can anyone please help me in writing this piece of code:( (1 Reply)
Discussion started by: Rajesh Putnala
1 Replies

2. UNIX for Advanced & Expert Users

mysql select query optimization..

hi.. i need to optimize my select query .. my situation is like this .. i have 15 lac recors in my table.. the following query takes nine seconds to give the required output.. SELECT max(ah.AUC_AMT), SUBSTRING_INDEX(GROUP_CONCAT(SUBSTRING_INDEX(ah.AUC_CUS_NAME,'@',1) order by AUC_AMT... (1 Reply)
Discussion started by: senkerth
1 Replies

3. Shell Programming and Scripting

mysql select query optimization..

hi.. i need to optimize my select query .. my situation is like this .. i have 15 lac recors in my table.. the following query takes nine seconds to give the required output.. SELECT max(ah.AUC_AMT), SUBSTRING_INDEX(GROUP_CONCAT(SUBSTRING_INDEX(ah.AUC_CUS_NAME,'@',1) order by AUC_AMT... (0 Replies)
Discussion started by: senkerth
0 Replies

4. Shell Programming and Scripting

Select query implement in a shell

I have been asked to create a shell script that accepts a number of SQL select queries as input, runs them in sequence, spools the output to an EXCEL workbook, where, each worksheet is an output of a Select statement run above. The workbook should be in a .XLS format. If a particular select... (2 Replies)
Discussion started by: ShellNovice1
2 Replies

5. Shell Programming and Scripting

Redirect Postgresql Select Query to Variable

How to redirect postgresql select query to a variable using shell scripts? I tried to use psql -c "select ip from servers" db | egrep '+\.+\+\+' | sed 's/^ *\(.*\) *$/\1/' Output: 10.10.10.180 10.10.10.181 It display ip addresses from the databasem, but how could i redirect the... (3 Replies)
Discussion started by: uativan
3 Replies

6. UNIX for Dummies Questions & Answers

Reading from a file and passing the value to a select query

Hi all, Here is my problem. I want to read data from a file and pass the variable to a select query. I tried but it doesn't seem to work. Please advise. Example below. FileName='filekey.txt' while read LINE do var=$LINE print "For File key $var" ${ORACLE_HOME}/bin/sqlplus -s... (1 Reply)
Discussion started by: er_ashu
1 Replies

7. Shell Programming and Scripting

how to convert the result of the select query to comma seperated data - urgent pls

how to convert the result of the select query to comma seperated data and put in a .csv file using korn shell. Pls help me as its very urgent. Thanks, Hema. (1 Reply)
Discussion started by: Hemamalini
1 Replies

8. Shell Programming and Scripting

Need Help (Select query)

Dear All, This may sound a simple query but a non technical person like me is not able to do it, So please help me out. I m using Unix... isql I jst wanted to do something like following select * from xyz where ID= xxxxxxxx (8 digit ID) Here if i put single 8 digit ID then the... (5 Replies)
Discussion started by: topgear1000cc
5 Replies

9. Shell Programming and Scripting

Displaying the data from the select query in a particular format

Hi, I have a shell script that returns 10 records for the column Name and age from a select query. Where when i store those data in retrieve_list.txt file i need to store the data in a particular format like:- $Jason$30 $Bill$23 $Roshan$25 Here is my script: 1)... (15 Replies)
Discussion started by: sachin.tendulka
15 Replies

10. Shell Programming and Scripting

Select a portion of file based on query

Hi friends :) I am having a small problem and ur help is needed... I have a long file from which i want to select only some portions after filtering (grep). My file looks like : header xxyy lmno xxyy wxyz footer header abcd xy pqrs footer . . (14 Replies)
Discussion started by: vanand420
14 Replies
Login or Register to Ask a Question