Execute SQL script using Windows Task Scheduler

Ebates Coupons and Cash Back

Say, you have a SQL script ready, and you want that to run automatically without you kick starting it every single time. You can do this via ‘adding the script to Windows Task Scheduler’, and configuring it.

Example script (saved as test.sql):

set echo on
set time on
set timing on
spool C:\some_folder\logfile\test.log;
drop table test_table;
create table test_table
(name varchar2(30), age int);
insert into test_table (name, age)
select ‘senthamizh selvan’, 20 from dual
union
select ‘Kanmani Anbodu’, 35 from dual;
commit;
spool off
exit;

Note: Spool commands where I’m writing the logs has the ‘Full Path’ of the log file.

We are going to use ‘sqlplus’ – a command line utility for Oracle Database / Query and Windows Task Scheduler to automate this SQL.

You can find ‘Task Scheduler’ application in the ‘Administrative Tools’ section (in Control Panels) for Windows server.

Screen Shot 2016-01-14 at 10.38.46 AM.png

Screen Shot 2016-01-14 at 10.38.56 AM.png

Open ‘Task Scheduler’ and Choose’Create Basic Task’ under Action.

Screen Shot 2016-01-14 at 10.39.39 AM.png

Give a Name to your task, and Click ‘Next’

 

Screen Shot 2016-01-14 at 10.40.36 AM.png

Choose a schedule -daily, weekly, etc ….

Screen Shot 2016-01-14 at 10.40.53 AM.png
And Click Next
Screen Shot 2016-01-14 at 10.41.28 AM.png
I chose ‘Daily’ and gave the ‘Time’ – Click Next once done
Screen Shot 2016-01-14 at 10.41.45 AM.png
Click Next
Screen Shot 2016-01-14 at 10.43.51 AM.png
Enter ‘sqlplus’ and in the arguments add “username/password@databse_name @C:\somefolder\….\test.sql”

Make sure you enter the full path of the location to the file in the arguments.

Screen Shot 2016-01-14 at 10.45.59 AM.png
Click Finish

And your job is ready to run.

Click on ‘Task Scheduler Library’ to confirm if your Job is there in the scheduler

Screen Shot 2016-01-14 at 10.47.39 AM.png

You can ‘right-click’ on the job to ‘Run’ manually once to verigy if its executing to completion or if you want to change any schedule/command/arguments – click on properties, and you will be able to change and Edit as needed.

Screen Shot 2016-01-14 at 10.47.55 AM.png

Screen Shot 2016-01-14 at 10.48.45 AM.png

Also, you can make the job to run in the server, even when you are not logged into the server, by choosing that option from the ‘General’ tab. But to do so, you should be an administrator or have the ‘batch’ access privilege.

See if you can make you ‘batch access’ by doing this

This policy is accessible by opening the Control Panel , Administrative Tools , and then Local Security Policy . In the Local Security Policy window, click Local Policy , User Rights Assignment , and then Logon as batch job

or you will be need to check with your system administrator

 

Screen Shot 2016-01-14 at 10.48.22 AM.png

Also, you can check your log file to confirm you are seeing what you are expecting to !!

 

 

Ebates Coupons and Cash Back

Leave a comment