SQL Server – Automatically Execute Query Every Hour in MS SQL Studio

scheduled-taskssql server

I support an application in a big enterprise, one of my roles is to clean-up data. There is a query I need to execute every hour, and I would like to automate it. Due to organization policies, I can't create SQL Server Agent jobs or modify schema, I can only manipulate data.

An endless

WHILE(1=1)
BEGIN 
WAITFOR DELAY '01:00';
--do work
END

does the job for me, but I shrug at the thought of a perma-open connection.

Ideally, I would script the MS SS itself to execute a given piece of code every hour, but I'm not sure if that is possible.

Is there any solution to this problem?

Best Answer

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.