Automatically execute query in MS SQL Studio every hour

Your friend is sqlcmd (Microsoft Technet)

  1. Create a SQL file with the script required to run your cleanup job
  2. Run the script with sqlcmd.exe and any required parameters
  3. Create a Windows Scheduled Task and add the command with all the required parameters

E.g.

sqlcmd -d YOUR_DB -E -i YOUR_SCRIPT.SQL -o OUTPUTFILE.TXT 

Good luck.


You don't need Management Studio to execute queries.

If you really have no option to have somebody schedule a job for you, you should look into sqlcmd as noted in hot2use's answer

If you are on a version where that is not supported (since you didn't specify a version) there is also osql which is a command line client, but that tool has been deprecated.

You could then schedule a command using any scheduler you want (Windows Task Scheduler for example) and run something like this:

OSQL -E -i c:\temp\dowork.sql

Have a look at the documentation to see which options you have for server selection and authentication.