Solved

SQL 2000 restore - terminate connections on target database

Posted on 2007-11-30
9
621 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 46

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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 46

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 46

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
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.

910 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

22 Experts available now in Live!

Get 1:1 Help Now