using SELECT sql statement in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using SELECT sql statement in shell script
# 1  
Old 06-17-2009
using SELECT sql statement in shell script

Hi there

I have a database on a remote box and i have been using shell script to insert data into it for example, i could have a script that did this

Code:
SN=123456
n=server1
m=x4140

sql="UPDATE main SET hostname='$n',model='$m' WHERE serial='$SN';"
echo $sql |/usr/sfw/bin/mysql -h db-server1 -utest TEST

this works fine and populates the database accordingly

however, I now want to be able to issue a SELECT statement (populating some variables with the results) so that i can continue to use them in the script

for example

if i was to issue

Code:
sql="SELECT hostname, model FROM main WHERE serial =$SN"
echo $sql |/usr/sfw/bin/mysql -h db-server1 -utest TEST

and somehow the 'hostname' value I retrieve will be put into say a variable called HN and the 'model' value I retrieve goes into a variable called MD

Does anybody know how I get these query results objects into usable variables like this ??

any help on this would be greatly appreciated as I am completely flummoxed
# 2  
Old 06-17-2009
this will get the values in the respective variable..
NOTE:Its better to set
set head off;
set feedback off;
set pages 0;
Code:
host=`echo "SELECT hostname FROM main WHERE serial =$SN"|/usr/sfw/bin/mysql -h db-server1 -utest TEST`
model=`echo "SELECT model FROM main WHERE serial =$SN"|/usr/sfw/bin/mysql -h db-server1 -utest TEST`

# 3  
Old 06-17-2009
thanks vidyadhar85

Unfortunately, I have hundreds of values ill be pulling down from the db, so technically i would have to have a line of code for each one, which isnt ideal but it works and thats great, thankyou

incidentally, issuing the "set head off" and the others, is that something I have to do on the server itself as a general setting , or do i integrate it into the query somehow ?

i tried
Code:
# echo "set head off" | /usr/sfw/bin/mysql -h db-server1 -utest TEST
ERROR 1193 at line 1: Unknown system variable 'head'

which didnt work
# 4  
Old 06-17-2009
then its better to take those hundreds of values into a flat file and then use them however you want
# 5  
Old 06-18-2009
thankyou , yes ill send it all out to a file (using -E to format the output vertically) then use some logic to pull those values into the script as variables

thank you for your help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Oop to copy and rename files through SQL Statement in shell Script

#!/bin/sh sqlplus -s "/ as sysdba" << EOF SET HEADING OFF SET FEEDBACK OFF Select pt.user_concurrent_program_name , OUTFILE_NAME FROm apps.fnd_concurrent_programs_tl pt, apps.fnd_concurrent_requests f where pt.concurrent_program_id = f.concurrent_program_id and pt.application_id =... (1 Reply)
Discussion started by: usman_oracle
1 Replies

2. Shell Programming and Scripting

Generate sql statement using shell scripting

Can anyone please assist me? I have attached 2 input files and one output file. I need to generate the sql update statements using the above 2 input files. if inputfile2 has 5 rows, then we should generate 5 update statements because column1 is unique. inputfile1 and inputfile2 may contain more... (10 Replies)
Discussion started by: vinus
10 Replies

3. Shell Programming and Scripting

How to create SQL statement out of data using shell script?

Table TAB1 contains following example data (its a tree sitting in table data format & its driven based CHILD & PARENT column pick the RULE condition to generate the below SQL: CHILD PARENT SS MID MNM VNM RULE FLG 1 ? S1 ? ? V1 rule004 I 2 1 S1 ? ? V1 0 Z 3 1 S1 ? ? V1 1 Z ... (6 Replies)
Discussion started by: gksenthilkumar
6 Replies

4. Shell Programming and Scripting

Parse SQL text and only format first SELECT statement.

Hi Forum. Need your expertise on the following question. I have the following file which I would like to parse, find first block of SELECT statment and concatenate all input fields as 1 field (~ delimited): Old File: SELECT /*+ USE_HASH(CCOMM ICAR IMAP IAS IP IMAS IMPS IAP SPCA) */ ... (5 Replies)
Discussion started by: pchang
5 Replies

5. Shell Programming and Scripting

Korn shell script - SQL statement challenges

Hi scripting experts. I have some coding challenges that I'm hoping you can help me out. I have one file#1 that contains the following sql statement that spans over multiple lines: sql Select /*+ use_has(a,b) */ * from customer a, customer_address b where a.id = b.id... (1 Reply)
Discussion started by: pchang
1 Replies

6. Shell Programming and Scripting

How to run a SQL select query in Oracle database through shell script?

I need to run a SQL select query in Oracle database and have to capture the list of retrieved records in shell script. Also i would like to modify the query for certain condition and need to fetch it again. How can i do this? Is there a way to have a persistent connection to oracle database... (9 Replies)
Discussion started by: vel4ever
9 Replies

7. Shell Programming and Scripting

sql select command output formatting in shell script

Hi, I need to connect to the database and retrieve two variables from the database and store them in a variable,out of these two variables I need to get lastdigit appended to the variable 1 retrieved and variable 2 with out any modification in short select var,data from usage; o/p=... (1 Reply)
Discussion started by: rkrish
1 Replies

8. Shell Programming and Scripting

Supress ' quotes in a select statement inside Shell Script

Hi I have a shell script in which a string variable is saving following query. SqlQuery="select a||'|'||b||'|'||c from dual" I am trying to pass above query as a parameter to some script but its giving me error for "single quote". I tried suppressing it using \' instead of just ' but it... (1 Reply)
Discussion started by: mahabunta
1 Replies

9. Windows & DOS: Issues & Discussions

Want to use the output of Select statement in Unix script

Hi, I have a UNIX script which calls SQL Select statement: Now i want to use the output of that select statement within my UNIX script so as to call different shell script depending upon the output of the select statement. Can anyone help me in this regard. TIA Akhil Goel (4 Replies)
Discussion started by: akhilgoel9
4 Replies

10. UNIX for Dummies Questions & Answers

Pipe SQL select statement results to script

Hello I would like to perform a select from a oracle table and return those values to my shell script For example: site=head -1 $infile | cut -c1-15 | awk '{printf "s%", $0} sqlplus -s /nolog |& #Open pipe to sql select col1, col2, col3, col4 from oracle_table where col5 =... (6 Replies)
Discussion started by: houtakker
6 Replies
Login or Register to Ask a Question