Visit The New, Modern Unix Linux Community


Query Oracle tables and return values to shell script that calls the query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Query Oracle tables and return values to shell script that calls the query
# 1  
Query Oracle tables and return values to shell script that calls the query

Hi,

I have a requirement as below which needs to be done viz UNIX shell script

(1) I have to connect to an Oracle database
(2) Exexute "SELECT field_status from table 1" query on one of the tables.
(3) Based on the result that I get from point (2), I have to update another table in the same oracle database. (eg., UPDATE table2 set column1='ABC' if the query in point2 retuns "MANUAL", if the query in point2 returns some other value for field_status then do not do anything on table2).


Could someone explain me how to acheive this ?? I Know how can this be acheived from UNIX script if the tables are in DB2 database but not sure how to write a UNIX script if tables are in Oracle database.



Thanks
Saaya
# 2  
Hi.

I'm not sure you need a shell script to do this, when you can do it in SQL / PL/SQL.

Code:
$ . oraenv
ORACLE_SID = [DB1] ? DB1

$ sqlplus user/pass

SQL*Plus: Release 10.2.0.3.0 - Production on Sun Oct 18 11:48:34 2009

Copyright (c) 1982, 2006, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Production
With the Partitioning and Data Mining options


SQL> create table A( F1 varchar2(32) )

Table created.

SQL> create table B( F2 varchar2(32) )

Table created.

SQL> insert into A values ( 'MANUAL' );

1 row created.

SQL> insert into A values ( 'SOME OTHER VALUE' );

1 row created.

SQL> commit;

Commit complete.

test.sql
================================
declare
  cursor MYCURSOR is select F1 from A;
begin
  for MYREC in MYCURSOR loop
    if ( MYREC.F1 = 'MANUAL' ) then
      insert into B( F2 ) values ( 'ABC' );
    end if;
  end loop;
end;
/
================================

$ sqlplus user/pass @test

SQL*Plus: Release 10.2.0.3.0 - Production on Sun Oct 18 11:47:58 2009

Copyright (c) 1982, 2006, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Production
With the Partitioning and Data Mining options

SQL> 
PL/SQL procedure successfully completed.


$ sqlplus user/pass

SQL*Plus: Release 10.2.0.3.0 - Production on Sun Oct 18 11:48:34 2009

Copyright (c) 1982, 2006, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Production
With the Partitioning and Data Mining options


SQL> select * from B;

F2
--------------------------------
ABC


Last edited by Scott; 10-21-2009 at 08:05 PM..
# 3  
hi

Thanks for the response....Yeah that is true.. I can easily do this in PL/SQl, but the requirement I hvae here is to do this viz UNIX shell script.. Hence I was checking how to get the same using shell script..
# 4  
In which case you need to qualify your select statement, or your question.

What if your select returns more than one record?

Code:
X=$(sqlplus -s user/pass << !
set heading off
set feedback off
select count(1) from A where F1 = 'MANUAL';
!)

echo $X

1


if [ $X -gt 0 ]; then
  sqlplus ......
  insert...
fi

# 5  
ok.. I just gave an example SELECT statement. My actual select statement has WHERE Clause which makes sure that only one row is returned at any point in time.
# 6  
Hi.

It helps if you post examples representative to your problem. This saves on the guess work.

My previous post shows how to return values from SQL/Plus into a shell variable.

If that's not enough, please do let me know.
# 7  
Hi All,
i need a script to select record from oracle and send mail
there are multiple records in the table i need to read row by row and send mail
pls help

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #408
Difficulty: Medium
The Cygwin distribution contains thousands of packages from the Open Source world including most GNU tools, many BSD tools, an X server and a full set of X applications.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script automation using cron which query's MySQL Tables

What I have: I have a input.sh (script which basically connect to mysql-db and query's multiple tables to write back the output to output1.out file in a directory) note: I need to pass an integer (unique_id = anything b/w 1- 1000) next to the script everytime I run the script which generates... (3 Replies)
Discussion started by: kkpand
3 Replies

2. Shell Programming and Scripting

Return db2 query value to shell script

Hi, Im new to DB2. I need to connect to DB2 from shell script and return the query output back to shell script variable. this is my code #!/bin/ksh db_name=db db_user=usr db_pwd=pwd db2 <<EOSQL connect to $db_name user $db_user using "$db_pwd" select count(1) from table quit EOSQL ... (3 Replies)
Discussion started by: sup
3 Replies

3. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

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

5. Shell Programming and Scripting

Problems with storing oracle sqlplus query output shell script

Hello everyone, I have a RHEL 5 system and have been trying to get a batch of 3-4 scripts each in a separate variables and they are not working as expected. I tried using following syntax which I saw a lot of people on this site use and should really work, though for some reason it doesn't... (3 Replies)
Discussion started by: rockf1bull
3 Replies

6. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

7. Shell Programming and Scripting

PROBLEM WITH ORACLE QUERY IN UNIX SCRIPT

hi Guys, i have a problem with oracle query in my unix script.. I'm getting the following error while executing.. ./logtab.sh: sqlplus -s "pmutv/pmutv1" << EOFSQL^Jset head off^Jinsert into... (2 Replies)
Discussion started by: apple2685
2 Replies

8. Shell Programming and Scripting

Shell script to query Oracle table

Hi, unix gurnis I need help for following requirement for writing a shell scritp. log in to oracle database, query one table total records (select count(*) from table1), pass the return value to a file. Thanks in advance (2 Replies)
Discussion started by: ken002
2 Replies

9. Shell Programming and Scripting

run oracle query remotly with shell script

hello how to run shell script to excute oracle queries on remote db ? i have tried as following sqlplus -s user/password@remote_server "select query;" but not working also this one sqlplus -s user/password@remote_server `select query;` not working :( i add this line to run another... (4 Replies)
Discussion started by: mogabr
4 Replies

10. Shell Programming and Scripting

Tables to query to find users for database from shell script

I am coding shell script. I need to connect to different databases like DB2, Oracle and Sybase. I would then need to query tables where it has all the groups, users for that database. I would also need who has what kind of permissions. EG: I know for DB2 some TABAUTH table needs to be... (0 Replies)
Discussion started by: pinnacle
0 Replies

Featured Tech Videos