cancel
Showing results for 
Search instead for 
Did you mean: 

SQL query in CRONTAB

SQL query in CRONTAB

Hi,
I want to get the data from a table and put them into a file (in the Operating System). I have a database over a Unix System. What's the command that do I have to ejecute?

Then, this command I want to program it in the crontab.

Thank you very much!!!

Programming a command SQL in the crontab
Christian Aguilar
11 REPLIES
harry d brown jr
Honored Contributor

Re: SQL query in CRONTAB

Look into writing a perl script:

http://www.princeton.edu/~storacle/perldbi.shtml

live free or die
harry
Live Free or Die
Steven E. Protter
Exalted Contributor

Re: SQL query in CRONTAB

Perl or shell scripting is the way to go. I am posting an example.

oracle/oracle@jufprod

Thats your oracle user id, oracle password and instance name.

Obviously, this is not a great thing to have in a script.

The book Perl Cookbook has methodology for dealing with the security issues in a better way.

And no, our oracle password is not oracle, just in case anyone wants to point out the obvious.

Steve
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Yogeeraj_1
Honored Contributor

Re: SQL query in CRONTAB

hi,

I have a script that convert a Oracle table into a flat file.
E.g.
============================================================
L-oracle8i: home/yd/flat>flat.sh scott/tiger emp
7369 SMITH CLERK 7902 17/12/1980 968 20
7499 ALLEN SALESMAN 7698 20/02/1981 1600 300 30
7521 WARD SALESMAN 7698 22/02/1981 1250 500 30
7566 JONES MANAGER 7839 02/04/1981 2975 20
7654 MARTIN SALESMAN 7698 28/09/1981 1250 1400 30
7698 BLAKE MANAGER 7839 01/05/1981 2850 30
7782 CLARK MANAGER 7839 09/06/1981 2450 10
7788 SCOTT ANALYST 7566 09/12/1982 3000 20
7839 KING PRESIDENT 17/11/1981 5000 10
7844 TURNER SALESMAN 7698 08/09/1981 1500 0 30
7876 ADAMS CLERK 7788 12/01/1983 1100 20
7900 JAMES CLERK 7698 03/12/1981 950 30
7902 FORD ANALYST 7566 03/12/1981 3000 20
7934 MILLER CLERK 7782 23/01/1982 1300 10
9876 REEVES ANALYST 7839 12/01/2085 3000 200 60
7999 PIERO CLERK 7839 20/02/1999 2000 30
7991 RAUL CLERK 7839 12/05/1991 1200 61
7989 HESKEY 1000 20

============================================================
Scripts are attached.

Now, if you want to "cron" it:
(the easiest way will be to use root account and switch user to run this script)
============================================================
# crontab -l
#*******************************************************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|--------------------------------------------------
00,30 08,09 * * * echo "/home/yd/flat/flat.sh scott/tiger emp dept > /home/yd/flat/myflatfile.txt"|su - oracle8i 1>/home/yd/flat/logfiles/output-flat.crn 2>/home/yd/flat/logfiles/error-flat.crn
#*******************************************************************************
# END OF TABLE day0->Sunday day6->Saturday
#*******************************************************************************
============================================================
(NB. this cronjob "exports" 2 tables emp and dept)

The output can also be formatted for Excel or any spreadsheet that understands the industry standard SYLK file format or even in SQLLOADER format that can be used to import in another database.

If you need any further help, please let use know.

Hope this helps!
Best Regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Yogeeraj_1
Honored Contributor

Re: SQL query in CRONTAB

hi again,

forgot the attachments and sorry for the formatting of the output ;)

Cheers
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Yogeeraj_1
Honored Contributor

Re: SQL query in CRONTAB

hi again,

the second file

regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Yogeeraj_1
Honored Contributor

Re: SQL query in CRONTAB

hi again,

the second file

regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)

Re: SQL query in CRONTAB

Oracle example.
Put an entry in crontab similar to:
00 15 * * 1-5 su - oracle -c "/path/script" > /dev/null 2>&1

The script should be something like:
sqlplus /nolog <select * from ....
select ...
EOF


Hope this helps,
Raimo

Re: SQL query in CRONTAB

Hi, yogeeraj:
Thank you by your suggestions.
I send a report about the table that I want to analize.
How Can I adapt it in your script flat.sh, Please?

I'd like that you send me the script with these parameters.

Thank you very much!!!
Christian Aguilar
Indira Aramandla
Honored Contributor

Re: SQL query in CRONTAB

Hi Christian,

You can spool the output of the select statements into a text file in the file system.
Eg:- Logon to SQLPLUS and the at the SQL prompt type this.

Spool test_report.txt
select from table_name
where ;
spool off
When you exit from SQLPLUS you list for test_report.txt file, you will see az text file and this will have the selected field name. You can format them as well.

Then cron job this script with the complete path to be executed daily / weekly.... depending on your requirements.
Never give up, Keep Trying
Indira Aramandla
Honored Contributor

Re: SQL query in CRONTAB

I am sending an attachment as an example. Hope this helps.


Indira
Never give up, Keep Trying
Yogeeraj_1
Honored Contributor

Re: SQL query in CRONTAB

hi Christian,

For the changes you are requesting, i think it will be preferable to create a view and use the script i sent you - This will not incur any changes in the scripts!

E.g.
create or replace view v_equipoid_101 as
select nombreequipo from equipo where idtipoequipo=101;

then use argument "v_equipoid_101" to the "flat" script!

Let me know if ok.

Also, if you post additional information on what you are planning to with the file, maybe i can suggest other alternatives.

Hope this help!

Best Regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)