I have come across with a doubt regarding stopping (killing) a query:
There is a procedure that changes the amount of data that a subscriber replicates (I don’t know all the details, it is some functionality that has been already implemented by the company), it’s in a transaction, so if it not finished it will rollback, while performing this procedure the replication for all subscribers is blocked, that is why we perform this operation during the night when the amount of subscribers replicating will be less or none. It is the weekend and I want to leave running the procedure (Friday 10pm) but I would like it to rollback if it doesn’t finish e.g. at 6am on Saturday and without the need to go to the office in order to stop the procedure manually.
setting it to run at 10pm is easy I have used
waitfor time '22:00'
I’m aware that in the same query can’t be a script that can stop the whole query since it is “sequential”, is there a way to do it opening other query tab? I hope that creating a job is not the only solution (if it is a solution at all).
Thank you for your replies.
I suggest the easiest way to handle this is by putting your long-running process into a job. Doesn’t require an open instance of Management Studio with a reliably active network connection to the server from your workstation, and since a job can only be running one instance at a time, it will be much easier work identifying the actual process that is running the job (and deal with it accordingly).
So let’s say I’ve convinced you, and you have a job called “Job I wanna kill” that is scheduled to run Friday night at 10 PM. The following stored procedure can be scheduled as a separate job, Saturday morning at 6:00 AM, or called manually.
When the job is killed successfully, the following will be seen as a printout when called manually or in the job step history when scheduled:
Now, there could be other complications – sometimes a rollback can take just as long as (or longer than) the time it took to get to that point. It all depends on what the long-running process is doing (I’m going to assume you’re rebuilding / reorganizing indexes).