Solved

Shell Script

Posted on 2008-10-08
15
773 Views
Last Modified: 2013-12-26
SHELL SCRIPT

We have to create a shell script with an Oracle INSERT command to insert rows into Table B from Table A every night with the following two conditions:

1. where users NOT IN XX and

2. based on SYSDATE

This shell script will be scheduled to run every night through the cron.

This is an urgent task.  Please respond at your earliest convenience.

Thanks!
0
Comment
Question by:as93is
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +2
15 Comments
 
LVL 1

Expert Comment

by:rgoble
ID: 22675521
Assuming that XX isn't changing you can modify the following SQL to use the correct table names and adjust the where clause.

INSERT INTO TABLE_B (SELECT * FROM TABLE_A WHERE users NOT IN XX);

After updating that line save it to a somefile.sql (name it something meaningful so you know whats its for later).

Next create your shell script (I know this works on windows, the syntax my differ on linux) with the following line:

sqlplus username/password @"somefile.sql"
0
 

Author Comment

by:as93is
ID: 22677050
The suggestions are useful.

Please give me a sample of a complete shell script.  I am new to AIX.

I want to use this shell script in the cron job.

I want to try this script and finalize it today.  This is an high priority task.

Thanks!!!
0
 
LVL 6

Expert Comment

by:peter991
ID: 22677094
This is on way..

#! /bin/ksh
sqlplus username/password >> /var/tmp/sqllogg.log 2>&1 <<!
@/oracle/sqlscripts/somefile.sql
exit
!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:as93is
ID: 22677132
I am new to AIX

Please explain what the following code will do.  This is code from your example:

>> /var/tmp/sqllogg.log 2>&1 <<!  

Also, could you please suggest another way in case the above doesn't work in my environment.

Thanks!!!
0
 
LVL 6

Expert Comment

by:peter991
ID: 22677309
/var/tmp/sqllogg.log is the logfile from the sqlscript you are running.
In other words.. You can check how the sql-job went.
It's good to create a log if you are running the job through the crontab.
0
 

Author Comment

by:as93is
ID: 22677332
Thank you very much for your quick reponse with useful sample code and explanation.

I will try this and get back to you.

Thanks!!!
0
 
LVL 10

Expert Comment

by:dbmullen
ID: 22678168
why is the schell script really a requirement?  
you can create a procedure and schedule a dbms_job.
then, it's in the database

CREATE OR REPLACE PROCEDURE why_use_shell_script
IS
BEGIN
 
  INSERT INTO TABLE_B (SELECT * FROM TABLE_A WHERE users NOT IN XX);
  commit;
  
END;
/
 
DECLARE
  X NUMBER;
BEGIN
  SYS.DBMS_JOB.SUBMIT
  ( job       => X 
   ,what      => 'why_use_shell_script;'
   ,next_date => to_date('20081009','yyyymmdd')
   ,interval  => 'TRUNC(SYSDATE) + 1 + 8/24 + 30/1440'
   ,no_parse  => FALSE
  );
  SYS.DBMS_OUTPUT.PUT_LINE('Job Number is: ' || to_char(x));
COMMIT;
END;
/

Open in new window

0
 

Author Comment

by:as93is
ID: 22685318
There is TABLE_A in the ORACLE database.  There is a DATABASE VIEW based on TABLE_A.  
The platform is AIX.

Here are the REQUIREMENTS:

1.  EXPORT TABLE_A data EVERY NIGHT.

2.  EVERY NIGHT AFTER THE EXPORT

     INSERT into TABLE_B (SELECT a, b, c
                         FROM VIEW
                         WHERE user NOT IN XXX);

3.  EVERY NIGHT AFTER THE INSERT
    TRUNCATE TABLE_A.

STEP 1 - CREATE THREE SHELL SCRIPTS

I thought that I could create a shell script for each of the above requirements as follows:

1.  One SHELL SCRIPT for EXPORT

2.  One SHELL SCRIPT for INSERT with the INSERT statement in a SQL script

3.  One SHELL SCRIPT for TRUNCATING TABLE_A.

STEP 2 - SET UP THREE CRON JOBS

SET UP THREE CRON JOBS TO RUN THE ABOVE THREE SHELL SCRIPTS EVERY NIGHT WITH A GAP OF HALF AN HOUR.

Please let me know if I've to SET VARIABLES in each of the above shell scripts.

Please let me know how best to meet the above requirements with examples/sample code ASAP.  
This is a high priority task with a strict deadline - 10-13-2008.

Thanks.
0
 
LVL 6

Expert Comment

by:peter991
ID: 22685611
Hello!

"Please let me know if I've to SET VARIABLES in each of the above shell scripts."

Yes!

Regards.
0
 

Author Comment

by:as93is
ID: 22691531
Hi peter991,

This is NOT THE SOLUTION that I expected.  

The solution that I expected was help to create the SHELL SCRIPTS for STEP 1 and STEP 2.

My question WAS NOT "Please let me know if I've to SET VARIABLES in each of the above shell scripts."

There is TABLE_A in the ORACLE database.  There is a DATABASE VIEW based on TABLE_A.  
The platform is AIX.

Here are the REQUIREMENTS:

1.  EXPORT TABLE_A data EVERY NIGHT.

2.  EVERY NIGHT AFTER THE EXPORT

     INSERT into TABLE_B (SELECT a, b, c
                         FROM VIEW
                         WHERE user NOT IN XXX);

3.  EVERY NIGHT AFTER THE INSERT
    TRUNCATE TABLE_A.

STEP 1 - CREATE THREE SHELL SCRIPTS

1.  One SHELL SCRIPT for EXPORT

2.  One SHELL SCRIPT for INSERT with the INSERT statement in a SQL script

3.  One SHELL SCRIPT for TRUNCATING TABLE_A.

STEP 2 - SET UP THREE CRON JOBS

SET UP THREE CRON JOBS TO RUN THE ABOVE THREE SHELL SCRIPTS EVERY NIGHT WITH A GAP OF HALF AN HOUR.

I need help to CREATE THE SHELL SCRIPTS FOR STEP 1 and STEP 2.  

Please let me know how best to meet the above requirements with examples/sample code ASAP.  
This is a high priority task with a strict deadline - 10-13-2008.

CAN SOMEONE HELP ME WITH THE ABOVE ASAP TO MEET MY DEADLINE?


Thanks.
0
 
LVL 35

Expert Comment

by:johnsone
ID: 22693932
Why 3 scripts?  As time goes on and things get bigger, these individual tasks may take more than 30 minutes to complete.

I just threw this together and it is not tested.  Some of the syntax may be incorrect as well, but it should be a good start.

You are going to have to fill in the ? with all the correct values for your system.
#!/bin/ksh
 
# Set Oracle environment
ORACLE_HOME=?
ORACLE_SID=?
PATH=${PATH}:${ORACLE_HOME}/bin
export ORACLE_HOME ORACLE_SID PATH
 
logfile="nightly_script.log.`date`"
 
(
#
# Export TABLE_A
#     NOTE:  This should log in as the owner of the table
#
exp userid=user/pass file=? tables=table_a compress=n
 
#
# Copy data to TABLE_B and truncate TABLE_A
#     NOTE:  This should log in as the owner of the table and the owner of both tables
#            is assumed to be the same
sqlplus << EOF
user/pass
WHENEVER SQLERROR EXIT;
INSERT into TABLE_B
     (SELECT a, b, c
        FROM VIEW
       WHERE user NOT IN XXX
     ); 
truncate table table_a;
exit
EOF 
) > "$logfile" 2>&1

Open in new window

0
 

Author Comment

by:as93is
ID: 22694573
Hi JOHNSONE,  

This is an excellent solution.  This is the kind of solution that I am looking for.  I will include all the correct values for my system and test it thoroughly.

I was thinking of three shell scripts for my three requirements.  You have combined all the three requirements into one shell script.  This is good.

WHAT WILL HAPPEN EXPORT or the INSERT FAILS in the shell script?

The EXPORT of TABLE_A AND INSERT INTO TABLE_B SHOULD to be successfully completed before I TRUNCATE TABLE_A.  

I will be in trouble if the EXPORT and INSERT fail and TABLE_A is truncated.  This is why I was thinking of three shell scripts instead of one.

There is a STEP 2 in my requirement.  I NEED HELP with the following:

SCHEDULE CRON JOB TO RUN the nightly_script.sh EVERY NIGHT AT 1 A.M.

I have to use the nightly_script.sh and schedule a cron job to run this shell script every night at 1 a.m.

Please help me with the CRON JOB code.  I have complete this by 10-13-200 (Monday).

I am thankful to you for your timely help!!!









0
 

Author Comment

by:as93is
ID: 22695815
WHAT WILL HAPPEN EXPORT or the INSERT FAILS in the shell script?

The EXPORT of TABLE_A AND INSERT INTO TABLE_B SHOULD to be successfully completed before I TRUNCATE TABLE_A.  

I will be in trouble if the EXPORT and INSERT fail and TABLE_A is truncated.  This is why I was thinking of three shell scripts instead of one.

There is a STEP 2 in my requirement.  I NEED HELP with the following:

SCHEDULE CRON JOB TO RUN the nightly_script.sh EVERY NIGHT AT 1 A.M.

I have to use the nightly_script.sh and schedule a cron job to run this shell script every night at 1 a.m.

Please help me with the CRON JOB code.  I have complete this by 10-13-200 (Monday).



0
 
LVL 35

Accepted Solution

by:
johnsone earned 500 total points
ID: 22710524
You will need to check the return code and exit if the exp fails.  Check the documentation, as there are different codes returned by exp that have different meanings.  You may not want to fail if you do get some of the codes.

If the insert fails, the script should exit.  That is what the WHENEVER statement should do.  If you want more robust error handling, encase the statements in an anonymous PL/SQL block and handle all the errors there.

This should be the line you need to put in the cron file:

0 1 * * * /full_path/nightly_script.sh
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question