开发者

Catch a cancelled stored procedure in SQL Server

I have a stored procedure in SQL server that looks something like this:

insert into Record(StartTimestamp) values (GETDATE())
SET @MyID = SCOPE_IDENTITY()
begin try
  -- do something
  UPDATE Record SET E开发者_运维知识库ndTimestamp = GETDATE() WHERE ID = @MyID
end try
begin catch
  UPDATE Record SET EndTimestamp = GETDATE(), Error = ERROR_MESSAGE() WHERE ID = @MyID
end catch

It gets called from an application and takes a few seconds to run. If a user cancels it while it's running I end up with a StartTimestamp in the Record table but no error and no EndTimestamp. I always want to know that the user initiated this stored proc but I also want to always record when it finished, either by success, error, or being cancelled.

Is there any way to do that in SQL Server?

Thanks


Maybe you could rework the cancellation logic? So instead of calling SqlCommand.Cancel from the business layer execute some second proc that sets a flag somewhere, then have the first proc check for that flag while it's running...

The whole point of SqlCommand.Cancel is that it really stops execution, stopping the SP from running and doing a rollback. I think you'll have to approach this from the business layer.

GJ

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜