Script to kill all connections to a database (More than RESTRICTED_USER ROLLBACK)

后端 未结 14 1563
-上瘾入骨i
-上瘾入骨i 2020-11-28 17:28

I have a development database that re-deploy frequently from a Visual Studio Database project (via a TFS Auto Build).

Sometimes when I run my build I get this error:

14条回答
  •  鱼传尺愫
    2020-11-28 17:52

    To my experience, using SINGLE_USER helps most of the times, however, one should be careful: I have experienced occasions in which between the time I start the SINGLE_USER command and the time it is finished... apparently another 'user' had gotten the SINGLE_USER access, not me. If that happens, you're in for a tough job trying to get the access to the database back (in my case, it was a specific service running for a software with SQL databases that got hold of the SINGLE_USER access before I did). What I think should be the most reliable way (can't vouch for it, but it is what I will test in the days to come), is actually:
    - stop services that may interfere with your access (if there are any)
    - use the 'kill' script above to close all connections
    - set the database to single_user immediately after that
    - then do the restore

提交回复
热议问题