Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL 2000 restore - terminate connections on target database

Posted on 2007-11-30
9
Medium Priority
?
666 Views
Last Modified: 2012-05-05
Hi,
I have a process which backs up my live database on server A and then restores on server B overnight. The problem is that sometimes the restore to server B fails as there are still user connections to the database I am restoring over the top of. What I need is some code to kill all the connections to a given database - as I know how to do this in enterprise manager but don't have the SQL.

I would suspect I need to query to pull info from somewhere to get the PID and then run a kill.

Any help appreciated
0
Comment
Question by:Netstore
7 Comments
 
LVL 43

Accepted Solution

by:
Eugene Z earned 672 total points
ID: 20381212
run before your restore code
----
use master
go

alter database yourdb
set SINGLE_USER with ROLLBACK IMMEDIATE

alter database yourdb
set MULTI_USER with NO_WAIT
0
 
LVL 4

Expert Comment

by:Sowmya_K
ID: 20381239
/*****************************************************************
*** Procedure: KillConnections
*** Usage: KillConnections @dbname = 'Database Name'
*** Description: Drop all connections from a specific database
*** Input: @dbname - REQUIRED - Name of the database
*** Output: Outputs the results of the proccess
******************************************************************/

create procedure KillConnections
      @dbname varchar(128)
as
      declare @spid varchar(5)
      declare @loginname nvarchar(128)
      declare @intErrorCode int
      declare @intOk int
      declare @intError int
      declare @intTotal int

      set @intErrorCode = 0
      set @intOk = 0
      set @intError = 0
      set @intTotal = 0

      select @intTotal = count(sp.spid) FROM master..sysprocesses sp
      JOIN master..sysdatabases sd ON sp.dbid = sd.dbid
      WHERE sd.name = @dbname
      
      declare KILL_CONS cursor for
      SELECT cast(sp.spid as varchar(5)),rtrim(sp.loginame)
      FROM master..sysprocesses sp
      JOIN master..sysdatabases sd ON sp.dbid = sd.dbid
      WHERE sd.name = @dbname
      
      OPEN KILL_CONS

      FETCH NEXT FROM KILL_CONS INTO @spid,@loginname
      WHILE @@FETCH_STATUS = 0
      BEGIN
            EXEC('Kill '+ @spid + '')
            SELECT @intErrorCode = @@ERROR
            if @intErrorCode = 0
            begin
                  set @intOk = @intOk + 1
                  PRINT 'Process ' + @spid + ' from login ' + @loginname + ' has been ended.'
            end
            else
            begin
                  set @intError = @intError + 1
                  PRINT 'Process ' + @spid + ' from login ' + @loginname + ' could not be ended.'
            end
            FETCH NEXT FROM KILL_CONS INTO @spid,@loginname
      END
      CLOSE KILL_CONS
      DEALLOCATE KILL_CONS
      PRINT 'Total number of processes from database ' + @dbname + ': ' + cast(@intTotal as varchar)
      PRINT 'Processes ended normally: ' + cast(@intOk as varchar)
      PRINT 'Processes could not be ended: ' + cast(@intError as varchar)
GO



Try this procedure , hope this helps !
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 20382685
EugeneZ it's easier and faster, but I think you won't need 2nd ALTER DATABASE because after you restore DB it will stay at same state when it was backed up.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 20382692
I wanted to say: "EugeneZ's solution it's easier and faster..." of course :)
0
 
LVL 5

Author Comment

by:Netstore
ID: 20394648
I used the kill connections sp (cheers!) - however on the rerstore I still get unable to gain exclusive lock on the database I am restoring over the top of - the only way to clear this was to bounce SQL server - which is a work around but not very professional. Any other idea's?
0
 
LVL 52

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 664 total points
ID: 20394680
Instead of SINGLE_USER try to use RESTRICTED_USER (dbo only):

alter database YourDBName SET RESTRICTED_USER with ROLLBACK IMMEDIATE
0
 
LVL 3

Assisted Solution

by:abhijit_k
abhijit_k earned 664 total points
ID: 20425824
i think while Restoring Use Master and the Same with Kill Connections and the Restore to Step2 as and when this is completed. the Restore Job should Fire we generally use this same as JOB
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question