Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a stored procedure using Windows task Scheduler

I've been trying to set up a schedule to run a stored procedure every hour in Windows Task Scheduler (as I'm using SQL Express and can't install 3rd party tools) but after trying various methods such as running a .bat file from task scheduler, opening SqlCmd utility from task scheduler and passing either the command line syntax or a .sql script file I'm having no luck.

I know this can be done and therefore I'm sure it's something I've missed but if anyone can share their experience of this I'd very much appreciate it.

The following command is in the batch file...

sqlcmd -E -i"C:\Users\Administrator\Desktop\test.sql" -o"C:\Users\Administrator\Desktop\dump.txt"

Thanks a lot

like image 430
richardterris Avatar asked Jan 14 '13 15:01

richardterris


People also ask

Can a stored procedure run on a schedule?

Still, there are few methods to schedule the execution of a stored procedure. For example, we can use the Windows Task Scheduler to schedule the execution of a script that consists of the execution of a procedure. For more detail, you can refer to the SQL Server schedule stored procedure without an agent.

How do I automatically execute a stored procedure in SQL?

In Object Explorer, connect to an instance of the SQL Server Database Engine, expand that instance, and then expand Databases. Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure.


1 Answers

If you are an admin on the sql instance (Since you are using SQLExpress I bet you are trying to do this on your own computer so there is a high chance your user is an admin of the sql instance) you should not use -E at all, just ignore it.

Second, specify the server even if you are working on local.

Start with a simple sql command like below:

sqlcmd.exe -S "." -d MY_DATABASE -Q "SELECT * FROM MY_TABLE"

Replace MY_DATABASE and MY_TABLE with your dbname and table name. Make sure you can run it from command line. It should return the data from your table. (Beware command line options are case-sensitive so -s is not same as -S)

Last, do not try to feed parameters through task scheduler. Put the command with all parameters in a .bat file and just run the batch from task scheduler.

like image 163
e-mre Avatar answered Sep 18 '22 17:09

e-mre