whoopwhoop2015
asked on
How can I create a cronjob that runs a SQL script with removes old records?
I have an sql query which clears out records older than 45 days in oracle sql database. I need to setup a cron job to run this sql query against 6 different database and have it run daily.
Database Names:
db1
db2
db3
db4
db5
db6
Usernames are different for each database, but passwords are the same.
Database Names:
db1
db2
db3
db4
db5
db6
Usernames are different for each database, but passwords are the same.
You can use oracle DBMS schedules
ASKER
Need to go through ksh scripting to keep things standard unfortunately. Need a ksh file to execute sql script for 6 different databases, which are all located on the same remote server. Need to run sql script against one database first and once the first database is complete it will run the sql script against the remaining 5 database at the same time.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It will be 10x faster if ran inside database.
t is way too many dependent parts, namely "remote" - all the network, and other machine that runs the scripts.
Consider at least running ksh script on DB server itself.
t is way too many dependent parts, namely "remote" - all the network, and other machine that runs the scripts.
Consider at least running ksh script on DB server itself.
Once you have your script, here are some tips on how to schedule is as a crontab job:
https://www.experts-exchange.com/OS/Linux/A_17919-How-to-setup-a-crontab-job-in-Linux-Unix.html
https://www.experts-exchange.com/OS/Linux/A_17919-How-to-setup-a-crontab-job-in-Linux-Unix.html
I'm having a hard time trying to figure out how a delete, which runs completely within the database and generates no return traffic, would run 10x faster through DBMS_JOB than through a SQL script. Remotely or not.
ASKER
Thank you johnsone, so something like this should work?
#!/bin/ksh
#Set Oracle environment here. Not sure how you do that. Some people use oraenv.
#Remember that environment is not set in cron, you need to set it yourself.
#At a minimum, you need ORACLE_HOME set
common_pass=password
for db_user in db1/user1 db2/user2 db3/user3 db4/user4 db5/user5 db6/user6
do
db=`echo $db_user | cut -f1 -d/`
user=`echo $db_user | cut -f2 -d/`
$ORACLE_HOME/bin/sqlplus -s ${user}/${common_pass}@(description=(address_list=(address=(protocol=TCP)(host=111.111.111.111)(port=1000)))(connect_data=(service_name=testservicename))) << EOF
"/test/testDelete.sql";
EOF
done
I'm confused as to why you aren't using the tns name. Are all the databases on the same machine? That would go against your remote statement and I don't see where you are using different databases.
Also, within sqlplus, to run the script would be;
@/test/testDelete.sql
That would assume that is the correct absolute path to the script.
Also, within sqlplus, to run the script would be;
@/test/testDelete.sql
That would assume that is the correct absolute path to the script.
ASKER
Two more questions and I will be done.
How can I set a value like deleteDate at top of script and pass it into sql?
I have another value I need to pass in that contains $/. like "fdasa$fdasf/.dfas" how can I have it ignore those characters for that variable and also pass that value in?
How can I set a value like deleteDate at top of script and pass it into sql?
I have another value I need to pass in that contains $/. like "fdasa$fdasf/.dfas" how can I have it ignore those characters for that variable and also pass that value in?
#!/bin/ksh
#Set Oracle environment here. Not sure how you do that. Some people use oraenv.
#Remember that environment is not set in cron, you need to set it yourself.
#At a minimum, you need ORACLE_HOME set
common_pass=password
deleteDate = 80
for db_user in db1/user1 db2/user2 db3/user3 db4/user4 db5/user5 db6/user6
do
db=`echo $db_user | cut -f1 -d/`
user=`echo $db_user | cut -f2 -d/`
$ORACLE_HOME/bin/sqlplus ${user}/${common_pass}@${db} << EOF
select * from tab where date = deleteDate;
EOF
done