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

1

I need to run a SQL select query in an Oracle database and have to capture the list of retrieved records in a shell script. Also I would like to modify the query for certain conditions and need to fetch it again. How can I do this?

Is there a way to have a persistent connection to an Oracle database using a shell script?

newbie dev

Posted 2011-12-21T07:30:05.593

Reputation: 147

Answers

2

You can do something like the following:

#!/bin/bash

DatabaseconnectStr=user/password@ALIAS
Dynamic=DynamicContent

sqlplus -S ${DatabaseconnectStr} << EOF

spool results.out
SELECT * FROM YOUR_TABLE WHERE COLUMN = ${Dynamic} ;
spool off
exit;
EOF

Eli

Posted 2011-12-21T07:30:05.593

Reputation: 21

Anyone who uses this, please make sure the folder you're placing the script in is only readable by you. Alternatively, put the password in another file in a protected folder and include it in the script. Still, I wouldn't do this in production. – Patrick R. – 2017-02-10T16:51:26.987

A way to get around that issue is having a tool like Jenkins call the script and store the passwords there so that it's not visible to others unless granted. – Eli – 2017-03-14T23:38:28.677