How do I read sql query into shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I read sql query into shell script?
# 1  
Old 06-03-2014
How do I read sql query into shell script?

Hello All,

I'm trying to put together a shell script that will:

1. connect to an oracle database
2. execute a query
3. save the output to a csv file

I know that I can execute the sqlplus -s user/pass @dbsid and get logged in. What I would like to do is have my query in a separate text file and read that file into the shell script to query the database. How do I do this?

Something like:

Code:
#!/bin/bash
sqlplus -s user/pass@oracle_sid
query=`cat file_with_query`

I could also use the <<EOF/EOF and pass the queries this way?:

Code:
#!/bin/bash
sqlplus -s user/pass@oracle_sid <<EOF
      {enter my query here}        
      exit;
EOF

# 2  
Old 06-03-2014
Try redirecting the query into it, sqlplus ... < file
# 3  
Old 06-04-2014
To insert your SQL, you could set up a variable and use that.

Could I suggest that you move you credentials from the sqlplus command line more like this:-
Code:
#!/bin/bash

while read line
do
   my_sql="$my_sql\n$line"
done < file

sqlplus -s <<-EOSQL
   $user/$pass@$sid
   $my_sql
EOSQL

If you choose not to, then whilst your sqlplus process is running, anyone could execute a ps command and see your database credentials.


I hope that this helps,
Robin
# 4  
Old 06-04-2014
Another couple of ways:
Code:
#!/bin/bash
sqlplus -s user/pass@oracle_sid @file.sql

(note that file.sql will need to contain an EXIT, or it will just hang)

Code:
#!/bin/bash
sqlplus -s user/pass@oracle_sid <<SQL
   @file.sql
   EXIT
SQL

# 5  
Old 06-04-2014
You are still hanging out your credentials for anyone to see with a simple ps though.....
# 6  
Old 06-04-2014
This is good stuff guys. Thanks!

I don't mind having the user/pass shown as the user I'm using have strictly a read-only access but I get what you're saying (it's good practice not to have passwords in the clear).

So the code will look like:

Code:
#!/bin/bash
user=sqluser
pass=sqlpass
while read line
do
   my_sql="$my_sql\n$line"
done < file

sqlplus -s <<-EOSQL
   $user/$pass@$sid
   $my_sql
EOSQL

And execute it using:

Code:
./mysqlqueryscript > results.csv

?
# 7  
Old 06-04-2014
Looks fine to me, except you haven't defined $sid

Personally, I would still keep credentials hidden. If, at a later stage, you get read/write or indeed you may every use the account to read something sensitive to either the company, customers or under any data protection laws, then you can imagine that letting them spread now could be problematic later, especially if you want to prove who did what later.

Maybe it's just my paranoia working for a financial company looking after the data of millions of real people and being bound up in UK laws to (quite rightly) protect them. Smilie


Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to execute sql query.

Hi Experts, Need your support. Not able to use sql query alias in shell script. Could you please help me in finding right way to use alias with sql query in shell script. Below is the code i am using. #!/bin/bash sqlplus -s abc/abc@abc << EOF> bcd.csv set trimspool on select zone_id... (4 Replies)
Discussion started by: as7951
4 Replies

2. Shell Programming and Scripting

How to embed sql query into our shell script?

Hi I would like to embed a sql query in my shell script. Also, before any the sql query is executed, i would like to validate username and password. (1 Reply)
Discussion started by: arghadeep adity
1 Replies

3. Shell Programming and Scripting

Convert the SQL Query in Shell Script

Hi All, I have a query with output below select 'create synonym "'||TABLE_NAME||'" for '||Table_owner||'."'||table_name||'"'||chr(59) from user_synonyms; ================== create synonym "RV_SBC_SIG" for WFCONTROLLER_TE."RV_SBC_SIG"; create synonym "AQM_TASK" for AWQM_TE."AQM_TASK";... (2 Replies)
Discussion started by: pvmanikandan
2 Replies

4. Red Hat

Sql query through shell script

hey , i am using this code to store value of a sql query and and then use it in other query but after some time , but it is not working. please help #!/bin/bash val_1=$( sqlplus -s rte/rted2@rel76d2 << EOF setting heading off select max(stat_id) from cvt_stats; exit EOF ) nohup... (5 Replies)
Discussion started by: ramsavi
5 Replies

5. UNIX for Dummies Questions & Answers

Regarding executing sql query in shell script

Hi, I have one SQL file prepared in UNIX and one script that is executing that. In SQL i have Update and create queries. I want to introduce conditions in SQL file (in UNIX) that if either of the create or update query failes whole transaction should be rollback. I just have 1 create... (2 Replies)
Discussion started by: abhii
2 Replies

6. Shell Programming and Scripting

$ symbol in sql query in shell script

Hi Team, Can you please help me to resolve this issue. Am unable to use this $ symbol in sql query in the shell script. For Example: # !/bin/sh export USER_NAME=XXX export PASSWORD=YYY export ORACLE_SID=xamdb echo $ORACLE_SID echo " Session Details ..." ... (1 Reply)
Discussion started by: indira_s
1 Replies

7. Shell Programming and Scripting

query sql using shell script

query sql using shell script, is it possible? my friend told me to do a file.sql and link to my shell script, but can i query sql using shell script? thanks in advance! (2 Replies)
Discussion started by: kingpeejay
2 Replies

8. Shell Programming and Scripting

executing a SQL query in shell script

Hi ALL, I need an help in connecting to oracle database, executing a select query and printing it on the screen. Can any one please write a simple code or psuedo code and let me know. select query returns multiple values( say select name from emp) Thanks in advance LM (1 Reply)
Discussion started by: lijju.mathew
1 Replies

9. Shell Programming and Scripting

Executing Sql Query Using Shell Script

HI ALL i have a requirement like this. i have to write a shell script to run a sql query. DB is oracle. once the query is run, the results of the query has to be published in a data file. can you please advice me how to go about it. i am absolutely new to shell scripts and this is a part of my job. (14 Replies)
Discussion started by: ragha81
14 Replies

10. UNIX for Dummies Questions & Answers

Executing a SQL query from a shell script

I cannot figure out how to run a SQL script, or just a sqlplus query, from a shell script (bash or ksh). Basically, I need to su - oracle from root and run a query, then test the exit status. (3 Replies)
Discussion started by: 98_1LE
3 Replies
Login or Register to Ask a Question