user5245950
user5245950

Reputation:

T-SQL Perform scheduled update via script in SQL Server 2012

For my production environment, everyday at 9 p.m I have to log in and manually run this script for some specific databases:

Update table
set status = 'Pending'
Where dbid = x

Is there a way to schedule this kind of update from the script so it gets executed at the desired time? Something like:

if time = 9:00 then update table...

I've read about the SQL Server agent job/task scheduler but I am trying to avoid this option if possible.

Upvotes: 0

Views: 74

Answers (1)

Moe Sisko
Moe Sisko

Reputation: 12051

If you don't want to use task schedulers etc, then one option might be to use batch files. Rather clunky, but might be worth trying out.

e.g. In the batch file, use the TIMEOUT command to wait for a specified number of seconds. You'll have to work out the required number of seconds based on how many seconds it will be from the time you kick off the batch file, until 9pm. After TIMEOUT completes, run SQLCMD (or similar), to run your desired sql.

Another option wholly inside TSQL might be to use the WAITFOR TSQL command. Kick off some SQL from the required database in SSMS, which looks something like:

WAITFOR TIME '21:00'  -- wait until 9pm
-- do your update table here

But note that you could be holding onto the sql connection for a long time.

Upvotes: 0

Related Questions