Help with Executing sql in Shell Script

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help with Executing sql in Shell Script
# 1  
Old 01-13-2018
Help with Executing sql in Shell Script

Hello~

I have a requirement to write a shell script which will connect to the oracle database and run a select count(*) query on a table.

The script should succeed only when the count returns a number greater than zero. If the count returns zero, the script should fail.

Can someone please walk me through the steps ?

Cheers,
Thanks in advance.
# 2  
Old 01-13-2018
Welcome to the forum.

The phrasing of your request leads to the assumption it might be homework.

Moderator's Comments:
Mod Comment Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.


Should the assumption be wrong, please post some evidence, e.g. the company you work for, or the project you're involved in.

Having said that, there are some helpful links on the bottom left of this page under "More UNIX and Linux Forum Topics You Might Find Helpful"
# 3  
Old 01-13-2018
Thank you.

I work for a company that offers Life,Travel,Auto and Home Insurance. I am an ETL developer and i majorly work on Informatica and sql but occasionally use Unix. Currently we are in Agile environment and i am working on a story where i need to create a shell script(with the requirement i have mentioned) and run the script before the Informatica job runs. I have worked on writing the shell scripts to pull or push a file, create a parameter file but not en corporate a sql query in the shell script.

Hence the post Smilie
# 4  
Old 01-14-2018
Hi,

The approach typically taken here is to use the Oracle-provided sqlplus command-line client, with your SQL query embedded in-line inside your shell script.

So for example, something like this:

Code:
#!/bin/bash

output=`sqlplus -silent username/password <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF
SELECT * FROM table WHERE column='value';
EXIT;
EOF`

echo The result of the Oracle SQL query was: "$output"

All of the above assumes you have a properly-configured Oracle command-line environment, there are already environment variables and/or config files in existence for the client that define the details of your Oracle database instance, and so on. Likewise, the options you SET depend entirely on what format you want your output in and how you want sqlplus to behave.

But in terms of demonstrating the general approach, the above gives you a fairly straightforward example that you can them customise. You may very well find you need to parse the output of the SQL query further in order to use it for whatever you want to do next, but hopefully this is enough to get you started.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Executing set of sql queries from shell script

Hi All, I tried executing set of queries from shell script but not able to capture the input query in the log file. The code looks something similar to below sqlplus user/pwd@dbname << EOF > output.log $(<inputfile.txt) EOF The above code is capturing the output of queries into... (9 Replies)
Discussion started by: loggedin.ksh
9 Replies

3. Shell Programming and Scripting

Executing a shell script from a PL / SQL Block

Hi, I need to call a shell script present on solaris server from within a PL / SQL block. Kindly suggest.. Thanks Sudhir (1 Reply)
Discussion started by: sudhird
1 Replies

4. UNIX for Dummies Questions & Answers

executing SQL query using unix shell script

I want to perform few post-session success tasks like update a status to 'true' in one of the sql database table, update date values to current system date in one of the configuration table in sql. How do i achieve this in a post session command?syntax with example will be helpful. (3 Replies)
Discussion started by: nathanvaithi
3 Replies

5. Shell Programming and Scripting

Executing Multiple .SQL Files from Single Shell Script file

Hi, Please help me out. I have around 700 sql files to execute in a defined order, how can i do it from shell script (3 Replies)
Discussion started by: anushilrai
3 Replies

6. Shell Programming and Scripting

Script executing sql query

Hello, I have a sh script excuting a sql query through sqlplus. I am having trouble making my date equal to the date of the server time in the sql script. How can i call the server date from my query? Thanks (2 Replies)
Discussion started by: kingluke
2 Replies

7. 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

8. 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

9. UNIX for Dummies Questions & Answers

Sh Shell Script executing remote SQL queries

Hi there folks, I am trying to execute remote sql queries on an Oracle server. I would like to save the result of the executed sql queries on a text file, and send that text file as an attachment to an email address. Could anyone give me an idea on how the above could be achieved? Any help... (2 Replies)
Discussion started by: Javed
2 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