Solved

SQL 2000 restore - terminate connections on target database

Posted on 2007-11-30
9
632 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
9 Comments
 
LVL 42

Accepted Solution

by:
EugeneZ earned 168 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 47

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 47

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 47

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 166 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 166 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

809 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