Select SQL Queries Option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select SQL Queries Option
# 1  
Old 05-29-2009
Select SQL Queries Option

count.sh
Code:
#!/bin/ksh


SQL1=`sqlplus -s usr/pwd @count.sql $1 $2 $3`
SQL2=`sqlplus -s usr/pwd @selectall.sql $1 $2 $3`

LIST="Count Select_All"

select i in $LIST
do
if   [ $i = "Count" ]
then
      echo $SQL1
elif [ $i = "Select_All" ]
  then
      echo $SQL2


fi
done
break
#END

count.sql

Code:
select count(*) from table
where result='&1' and status='&2' and x_tran_datetime='&date';

exit

Could somebody help me with this. I want to create a script that could give query option to user, one to select & another one to count.

User choose either one option,then it will request for parameters,when the parameters inserted then it will give the output. Please help. Thanks
# 2  
Old 05-29-2009
Quote:
Originally Posted by killboy
... I want to create a script that could give query option to user, one to select & another one to count.
User choose either one option,then it will request for parameters,when the parameters inserted then it will give the output. ...
Something like this:

Code:
$ 
$ cat select_all.sql 
set feed off verify off heading off pages 0
select * from emp where 1=&1 and 2=&2;     
exit                                       

$ 
$ cat count.sql 
set feed off verify off heading off pages 0
select count(*) from emp where 1=&1 and 2=&2 and 3=&3;
exit                                                  

$ 
$ cat get_data.sh 
#!/usr/bin/bash   
ch=""; p1=""; p2=""; p3=""
echo "1)  Get all data"   
echo "2)  Get the count"  
echo "Enter your choice : "
read ch                    
case "$ch" in              
  "1") echo "Enter parameter 1: "; read p1
       echo "Enter parameter 2: "; read p2 ;;
  "2") echo "Enter parameter 1: "; read p1   
       echo "Enter parameter 2: "; read p2   
       echo "Enter parameter 3: "; read p3 ;;
esac                                         

if [ "$ch" = "1" ]; then
  sqlplus -s test/test @select_all.sql $p1 $p2 >alldata.txt
  echo "All data =>"                                           
  cat alldata.txt                                              
elif [ "$ch" = "2" ]; then                                     
  count=$(sqlplus -s test/test @count.sql $p1 $p2 $p3)         
  echo "The count = $count"                                    
fi                                                             

$ 
$ . get_data.sh
1)  Get all data
2)  Get the count
Enter your choice : 
2                   
Enter parameter 1:  
1                   
Enter parameter 2:  
2                   
Enter parameter 3:  
3                   
The count =     13  
$                   
$                   
$ . get_data.sh
1)  Get all data
2)  Get the count
Enter your choice :
1
Enter parameter 1:
1
Enter parameter 2:
2
All data =>
      7369 SMITH      CLERK           7902 17-DEC-80                 800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81                1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81                1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81                2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81                1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81                2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81                2450                    10
      7788 SCOTT      ANALYST         7566 09-DEC-82                3000                    20
      7839 KING       PRESIDENT            17-NOV-81                5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81                1500          0         30
      7876 ADAMS      CLERK           7788 12-JAN-83                1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81                 950                    30
      7902 FORD       ANALYST         7566 03-DEC-81                3000                    20
$
$

HTH,
tyler_durden
# 3  
Old 05-30-2009
Thank you very much ,sir ....it works now Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

2. Programming

Join 2 SQL queries into one

Dear community, could someone help me to join 2 queries into one? Basically it's the same query with different clauses (please notice the FIELD3 filters and related counters into the subquery): SELECT A.FIELD1,A.FIELD2,A.FIELD3 FROM TABLE A INNER JOIN ( SELECT FIELD1,... (3 Replies)
Discussion started by: Lord Spectre
3 Replies

3. AIX

Convert MS Access Queries to AIX SQL

Unix Team, I'm a recent college graduate entering in the " real world" and boy let me tell you it can quite challenging. I just got a job with a large fortune 50 company lets just say that my expectations and tasks have expanded since I first got hired. Last week I got assigned a pretty big... (9 Replies)
Discussion started by: techstudent01
9 Replies

4. Shell Programming and Scripting

Code needed to get sql queries

Hi i need code to get sql queries through a shell script for a text file input which contain the service ids iputfile I-H-2048-10GB-M I-H-4096-12GB-M I-H-2048-p1000-M the code should contain below queries among which service_id is replacable with value from input file. ... (4 Replies)
Discussion started by: surender reddy
4 Replies

5. Shell Programming and Scripting

Script (with sql queries) not working using cron

Hi all, I have script, which performing sql queries and put output into file. When I run this script manually, its working fine, but when I want to schedule it with cron I am getting errors... I defined LD_LYBRARY_PATH and ,but no result. After I defined it, I am getting error: # more... (4 Replies)
Discussion started by: nypreH
4 Replies

6. Emergency UNIX and Linux Support

Insert data into sql queries from a file

Hello friends, I need to insert data from a file to another. I need this to form an sql query file which will consist of 50.000 INSERT INTO sentences. my sql query file will consist of 50.000 times the below line consecutively: insert into subscriber... (6 Replies)
Discussion started by: EAGL€
6 Replies

7. Shell Programming and Scripting

Nested SQL queries within Shell script

Hi, Would someone know if I can fire nested sql queries in a shell script? Basically what I am trying to do is as follows: my_sql=$(sqlplus -s /nolog<<EOF|sed -e "s/Connected. *//g" connect... (2 Replies)
Discussion started by: shrutihardas
2 Replies

8. UNIX for Dummies Questions & Answers

shell script for sql queries

Hi All, I have written 4 sql queries . Now I want to write one SHELL SCRIPTING program for all these queries... i.e 1.select * from head; 2. select * from detail; 3. delete from head; 4. delete from detail; Please let me know how to write a shell script... Thank you (1 Reply)
Discussion started by: user71408
1 Replies

9. Shell Programming and Scripting

Extracting select queries from script

Hi, I have a script in which a lot of sql queries are embedded ie,"Select .....;".My purpose is to document all the dml statements in the script along with the line number. I am thinking of writing a perlscript or by using awk/sed scripts for extracting all the 'select' statements/queries... (3 Replies)
Discussion started by: DILEEP410
3 Replies

10. UNIX for Dummies Questions & Answers

SQL queries in background?

I have to query a DB2 database, and sometimes they take a long time to produce results. Can I run these queries in the background, and if so, where will the results appear? (1 Reply)
Discussion started by: jpprial
1 Replies
Login or Register to Ask a Question