Multiple SQL Commands


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers Multiple SQL Commands
# 1  
Old 11-15-2010
Multiple SQL Commands

Hello Experts !!!

Im new to this so, please bear with me !

I have the following 2 sql statements
Code:
create table CMD_NEW_USER as
Select FD.EMPLOYEE, FD.PASTUSER, .....
FROM REPORTS FS, TASKLIST FD
WHERE FS.EMPLOYEEID = FD.EMPLOYEEID ;

create table CMD_OLD_USER as
Select FD.EMPLOYEE, FD.PASTUSER, .....
FROM REPORTS FS, TASKLIST FD
WHERE FS.EMPLOYEEID != FD.EMPLOYEEID ;

Now, here are the challenge's
1. I need to create a shell wrapper so that the above two sql create table commands run successfully by connecting to the correct database.
2. Run this everyday at 11 PM so that the next day this is done.

Any and all help is sincerely appreciated !

Thanks,
Lee

Last edited by Scott; 11-15-2010 at 07:46 PM.. Reason: Code tags
# 2  
Old 11-15-2010
You can do this a couple different ways. Here's one that will do all the work in the script.
Code:
#!/bin/ksh

sqlplus -s /nolog <<-EOF
   connect $USERNAME/$PASSWD@$DB

   spool $LOGFILE

   drop table cmd_new_user;

   create table cmd_new_user as
   Select fd.employee
         ,fd.pastuser
         ,...
     from reports fs
         ,tasklist fd
    where fs.employeeid = fd.employeeid;

   drop table cmd_old_user;

   create table cmd_old_user as
   Select fd.employee
         ,fd.pastuser
         ,...
     from reports fs, tasklist fd
    where fs.employeeid != fd.employeeid;

   spool off

   exit

EOF

exit 0

# 3  
Old 11-15-2010
You might want to throw in a "drop table" somewhere before the "create table" Smilie

Assuming Oracle:

In your script source your .profile, set ORAENV_ASK to NO, set your ORACLE_SID to that of the database you're planning to connect, and then source "oraenv".

Code:
$ cat my_script
. ~/.profile
ORAENV_ASK=NO
ORAENV=mydb
. oraenv

sqlplus /nolog << !
connect user/pass@db
your DDL goes here;
!

As for the "11 PM" part, set up a cronjob.

Code:
0 23 * * * /path_to_my_script/my_script


Last edited by Scott; 11-15-2010 at 08:27 PM.. Reason: Changed 11 to 23 on cronjob -- oops :)
# 4  
Old 11-15-2010
Scottn & Jsmitstl,

Thanks a bunch on your responses and Im thankful for those.

Scottn, You are right in that this is oracle that we are talkin about bu you lost me there on the following and Im not sure as what you mean here.

In your script source your .profile, set ORAENV_ASK to NO, set your ORACLE_SID to that of the database you're planning to connect, and then source "oraenv".


Code:
$ cat my_script
. ~/.profile
ORAENV_ASK=NO
ORAENV=mydb
. oraenv


Now, Im reading something about the Cron and I will certainly need some more time for what you have mentioned in here.

Thanks,
Lee.
# 5  
Old 11-15-2010
Hi.

I mean that when you run something from cron your environment is not set up as when you normally "log in".

You need to "set it up" yourself.

Sourcing (loading) your .profile is normally a good place to start
Code:
. ~/.profile

If that doesn't give you all you need, then:
Code:
PATH=/home/oracle/product/10.2.0.8/bin:$PATH

(or wherever your Oracle software "lives")

If you want to use SQL*Plus, then you need to set your Oracle environment. oraenv does this for you:
Code:
ORAENV_ASK=NO
ORACLE_SID=mydb
. oraenv

or:
Code:
ORACLE_SID=mydb  # Optional if you specify a DB name in your connect string, and have a running listener
ORACLE_HOME=/home/oracle/product/10.2.0.8

Login or Register to Ask a Question

Previous Thread | Next Thread

8 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. UNIX for Advanced & Expert Users

Pass Multiple Commands and Open Multiple Xterms via PSS

Hello, I'm attempting to open multiple xterms and run a command as an SAP user via sudo using PSSH. So far, I'm able to run PSSH to a file of servers with no check for keys, open every xterm in to the servers in the file list, and SUDO to the SAP(ADM) user, but it won't do anything else... (11 Replies)
Discussion started by: icemanj
11 Replies

3. Shell Programming and Scripting

Create Multiple UNIX Files for Multiple SQL Rows output

Dear All, I am trying to write a Unix Script which fires a sql query. The output of the sql query gives multiple rows. Each row should be saved in a separate Unix File. The number of rows of sql output can be variable. I am able save all the rows in one file but in separate files. Any... (14 Replies)
Discussion started by: Rahul_Bhasin
14 Replies

4. Shell Programming and Scripting

perform 3 awk commands to multiple files in multiple directories

Hi, I have a directory /home/datasets/ which contains a bunch (720) of subdirectories called hour_1/ hour_2/ etc..etc.. in each of these there is a single text file called (hour_1.txt in hour_1/ , hour_2.txt for hour_2/ etc..etc..) and i would like to do some text processing in them. Each of... (20 Replies)
Discussion started by: amarn
20 Replies

5. Shell Programming and Scripting

Execute multiple SQL scripts from single SQL Plus connection

Hi! I would like to do a single connection to sqlplus and execute some querys. Actually I do for every query one connection to database i.e echo 'select STATUS from v$instance; exit' > $SQL_FILE sqlplus user/pass@sid @$SQL_FILE > $SELECT_RESULT echo 'select VERSION from v$instance;... (6 Replies)
Discussion started by: guif
6 Replies

6. Shell Programming and Scripting

SQL commands in Scripting

Hi , Daily I sneding report to client about the status of State 2 jobs in Control M . I would like to schedule it via cron . I am using teh following command : sudo su - <control M server Name> then reach to the control M server then run #isql <It ask for the password > ... (1 Reply)
Discussion started by: chandancsc
1 Replies

7. Shell Programming and Scripting

how to sql commands in shell script

hi, plz let me know one example in using sql command in shell script thanks inadvance -bali. (1 Reply)
Discussion started by: balireddy_77
1 Replies

8. Shell Programming and Scripting

SQL Commands in Unix

Hi gurus, Being a newbie not sure whether iam asking the right question? can we use oracle commands in unix for ex: to read a count on a table. if so, what needs to be configured and etc.. any kind of help is appreciated. Thanks sish (6 Replies)
Discussion started by: sish78
6 Replies
Login or Register to Ask a Question