Solved

SQL 2000 restore - terminate connections on target database

Posted on 2007-11-30
9
656 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 43

Accepted Solution

by:
Eugene Z 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 51

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 51

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 51

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

623 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