Solved

SQL 2000 restore - terminate connections on target database

Posted on 2007-11-30
9
607 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
Comment Utility
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
Comment Utility
/*****************************************************************
*** 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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 45

Expert Comment

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

Author Comment

by:Netstore
Comment Utility
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 45

Assisted Solution

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now