Home All Groups Group Topic Archive Search About
Author
20 Nov 2005 8:27 AM
ad
I use the SQL:
"use master; restore database myBase from disk = 'c:\MyBackup.bak' with
Replace " in program
to restore myBase form the disk file MyBackup.bak, the disk file
MyBackup.bak is a Sql2005 backup file.
But it result a error:
Exclusive access could not be obtained because the database is in use.

I think there must have some connection still exist.

How can I disconnect all connections to myBase before execute the restore
command?

Author
20 Nov 2005 8:52 AM
luxspes
From http://www.dbmaint.com/download/util_proc/sp_dbm_kill_users.sql
you can use the following Stored Procedure:


USE master
GO
CREATE PROC sp_dbm_kill_users @dbname sysname, @delay DATETIME = '00:00' AS
/*******************************************************************************/
/* This procedure executes KILL for all connections in the specified
database. */
/* The procedure can be mofified so it kills all connections for a
server.     */
/* The procedure takes the following parameters:
        */
/*   @dbname SYSNAME (required): Database name.
        */
/*   @delay DATETIME (optional) [0] : Optional delay (s) before resume.
        */
/* Written by Tibor Karaszi and Börje Carlsson 1999. www.dbmaint.com
        */
/* Tested on verion 6.5, 7.0 and 8.0.
        */
/*******************************************************************************/
SET NOCOUNT ON
--Get version number and verify supported version
DECLARE @ver VARCHAR(7)
SELECT @ver = CASE
  WHEN CHARINDEX('6.50', @@VERSION) > 0 THEN '6.50'
  WHEN CHARINDEX('7.00', @@VERSION) > 0 THEN '7.00'
  WHEN CHARINDEX('8.00', @@VERSION) > 0 THEN '8.00'
  ELSE 'Unknown'
END
IF @ver = 'Unknown'
BEGIN
    RAISERROR('Unsupported version of SQL Server.',16,1)
    RETURN -101
END

DECLARE loop_name INSENSITIVE CURSOR FOR
   SELECT spid
    FROM master..sysprocesses
    WHERE dbid = DB_ID(@dbname)

OPEN loop_name
DECLARE @conn_id SMALLINT
DECLARE @exec_str VARCHAR(255)
FETCH NEXT FROM loop_name INTO @conn_id
WHILE (@@fetch_status = 0)
   BEGIN
     SELECT @exec_str = 'KILL ' + CONVERT(VARCHAR(7), @conn_id)
     EXEC( @exec_str )
     FETCH NEXT FROM loop_name INTO @conn_id
   END
DEALLOCATE loop_name

WAITFOR DELAY @delay
GO

/* Sample Execution:
EXEC sp_dbm_kill_users pubs
EXEC sp_dbm_kill_users @dbname = pubs, @delay = '00:00:05'
*/




ad wrote:
Show quote
> I use the SQL:
> "use master; restore database myBase from disk = 'c:\MyBackup.bak' with
> Replace " in program
> to restore myBase form the disk file MyBackup.bak, the disk file
> MyBackup.bak is a Sql2005 backup file.
> But it result a error:
> Exclusive access could not be obtained because the database is in use.
>
> I think there must have some connection still exist.
>
> How can I disconnect all connections to myBase before execute the restore
> command?
>
>
Author
20 Nov 2005 11:38 AM
ad
Thanks,
But I can't create stored procedure in the server.
Can I disconnect all connections by program?


"luxspes" <m*@privacy.net>
???????:OH9Aw%23a7FHA.2***@TK2MSFTNGP11.phx.gbl...
Show quote
> From http://www.dbmaint.com/download/util_proc/sp_dbm_kill_users.sql
> you can use the following Stored Procedure:
>
>
> USE master
> GO
> CREATE PROC sp_dbm_kill_users @dbname sysname, @delay DATETIME = '00:00'
> AS
> /*******************************************************************************/
> /* This procedure executes KILL for all connections in the specified
> database. */
> /* The procedure can be mofified so it kills all connections for a server.
> */
> /* The procedure takes the following parameters: */
> /*   @dbname SYSNAME (required): Database name. */
> /*   @delay DATETIME (optional) [0] : Optional delay (s) before resume. */
> /* Written by Tibor Karaszi and Börje Carlsson 1999. www.dbmaint.com */
> /* Tested on verion 6.5, 7.0 and 8.0. */
> /*******************************************************************************/
> SET NOCOUNT ON
> --Get version number and verify supported version
> DECLARE @ver VARCHAR(7)
> SELECT @ver = CASE
>  WHEN CHARINDEX('6.50', @@VERSION) > 0 THEN '6.50'
>  WHEN CHARINDEX('7.00', @@VERSION) > 0 THEN '7.00'
>  WHEN CHARINDEX('8.00', @@VERSION) > 0 THEN '8.00'
>  ELSE 'Unknown'
> END
> IF @ver = 'Unknown'
> BEGIN
>    RAISERROR('Unsupported version of SQL Server.',16,1)
>    RETURN -101
> END
>
> DECLARE loop_name INSENSITIVE CURSOR FOR
>   SELECT spid
>    FROM master..sysprocesses
>    WHERE dbid = DB_ID(@dbname)
>
> OPEN loop_name
> DECLARE @conn_id SMALLINT
> DECLARE @exec_str VARCHAR(255)
> FETCH NEXT FROM loop_name INTO @conn_id
> WHILE (@@fetch_status = 0)
>   BEGIN
>     SELECT @exec_str = 'KILL ' + CONVERT(VARCHAR(7), @conn_id)
>     EXEC( @exec_str )
>     FETCH NEXT FROM loop_name INTO @conn_id
>   END
> DEALLOCATE loop_name
>
> WAITFOR DELAY @delay
> GO
>
> /* Sample Execution:
> EXEC sp_dbm_kill_users pubs
> EXEC sp_dbm_kill_users @dbname = pubs, @delay = '00:00:05'
> */
>
>
>
>
> ad wrote:
>> I use the SQL:
>> "use master; restore database myBase from disk = 'c:\MyBackup.bak' with
>> Replace " in program
>> to restore myBase form the disk file MyBackup.bak, the disk file
>> MyBackup.bak is a Sql2005 backup file.
>> But it result a error:
>> Exclusive access could not be obtained because the database is in use.
>>
>> I think there must have some connection still exist.
>>
>> How can I disconnect all connections to myBase before execute the restore
>> command?
>>
Author
20 Nov 2005 2:04 PM
luxspes
In SQLServer 2000 With enterprise manager, All Tasks -> Detach Database
-> Dialg, that dialog allows you to disconnect everyone... (just
remember to only use the "disconnect" feature, or will have to re-Attach
de database)

ad wrote:
Show quote
> Thanks,
> But I can't create stored procedure in the server.
> Can I disconnect all connections by program?
>
>
> "luxspes" <m*@privacy.net>
> ???????:OH9Aw%23a7FHA.2***@TK2MSFTNGP11.phx.gbl...
>
>>From http://www.dbmaint.com/download/util_proc/sp_dbm_kill_users.sql
>>you can use the following Stored Procedure:
>>
>>
>>USE master
>>GO
>>CREATE PROC sp_dbm_kill_users @dbname sysname, @delay DATETIME = '00:00'
>>AS
>>/*******************************************************************************/
>>/* This procedure executes KILL for all connections in the specified
>>database. */
>>/* The procedure can be mofified so it kills all connections for a server.
>>*/
>>/* The procedure takes the following parameters: */
>>/*   @dbname SYSNAME (required): Database name. */
>>/*   @delay DATETIME (optional) [0] : Optional delay (s) before resume. */
>>/* Written by Tibor Karaszi and Börje Carlsson 1999. www.dbmaint.com */
>>/* Tested on verion 6.5, 7.0 and 8.0. */
>>/*******************************************************************************/
>>SET NOCOUNT ON
>>--Get version number and verify supported version
>>DECLARE @ver VARCHAR(7)
>>SELECT @ver = CASE
>> WHEN CHARINDEX('6.50', @@VERSION) > 0 THEN '6.50'
>> WHEN CHARINDEX('7.00', @@VERSION) > 0 THEN '7.00'
>> WHEN CHARINDEX('8.00', @@VERSION) > 0 THEN '8.00'
>> ELSE 'Unknown'
>>END
>>IF @ver = 'Unknown'
>>BEGIN
>>   RAISERROR('Unsupported version of SQL Server.',16,1)
>>   RETURN -101
>>END
>>
>>DECLARE loop_name INSENSITIVE CURSOR FOR
>>  SELECT spid
>>   FROM master..sysprocesses
>>   WHERE dbid = DB_ID(@dbname)
>>
>>OPEN loop_name
>>DECLARE @conn_id SMALLINT
>>DECLARE @exec_str VARCHAR(255)
>>FETCH NEXT FROM loop_name INTO @conn_id
>>WHILE (@@fetch_status = 0)
>>  BEGIN
>>    SELECT @exec_str = 'KILL ' + CONVERT(VARCHAR(7), @conn_id)
>>    EXEC( @exec_str )
>>    FETCH NEXT FROM loop_name INTO @conn_id
>>  END
>>DEALLOCATE loop_name
>>
>>WAITFOR DELAY @delay
>>GO
>>
>>/* Sample Execution:
>>EXEC sp_dbm_kill_users pubs
>>EXEC sp_dbm_kill_users @dbname = pubs, @delay = '00:00:05'
>>*/
>>
>>
>>
>>
>>ad wrote:
>>
>>>I use the SQL:
>>>"use master; restore database myBase from disk = 'c:\MyBackup.bak' with
>>>Replace " in program
>>>to restore myBase form the disk file MyBackup.bak, the disk file
>>>MyBackup.bak is a Sql2005 backup file.
>>>But it result a error:
>>>Exclusive access could not be obtained because the database is in use.
>>>
>>>I think there must have some connection still exist.
>>>
>>>How can I disconnect all connections to myBase before execute the restore
>>>command?
>>>
>
>
Author
20 Nov 2005 10:07 PM
ad
But I am use Sql2005 Express to develop my application.
The user can't use these tools.
How can I disconnect all connections by program?



Show quote
"luxspes" <m*@privacy.net> ???????:uAThEtd7FHA.2***@TK2MSFTNGP11.phx.gbl...
> In SQLServer 2000 With enterprise manager, All Tasks -> Detach Database ->
> Dialg, that dialog allows you to disconnect everyone... (just remember to
> only use the "disconnect" feature, or will have to re-Attach de database)
>
> ad wrote:
>> Thanks,
>> But I can't create stored procedure in the server.
>> Can I disconnect all connections by program?
>>
>>
>> "luxspes" <m*@privacy.net>
>> ???????:OH9Aw%23a7FHA.2***@TK2MSFTNGP11.phx.gbl...
>>
>>>From http://www.dbmaint.com/download/util_proc/sp_dbm_kill_users.sql
>>>you can use the following Stored Procedure:
>>>
>>>
>>>USE master
>>>GO
>>>CREATE PROC sp_dbm_kill_users @dbname sysname, @delay DATETIME = '00:00'
>>>AS
>>>/*******************************************************************************/
>>>/* This procedure executes KILL for all connections in the specified
>>>database. */
>>>/* The procedure can be mofified so it kills all connections for a
>>>server. */
>>>/* The procedure takes the following parameters: */
>>>/*   @dbname SYSNAME (required): Database name. */
>>>/*   @delay DATETIME (optional) [0] : Optional delay (s) before resume.
>>>*/
>>>/* Written by Tibor Karaszi and Börje Carlsson 1999. www.dbmaint.com */
>>>/* Tested on verion 6.5, 7.0 and 8.0. */
>>>/*******************************************************************************/
>>>SET NOCOUNT ON
>>>--Get version number and verify supported version
>>>DECLARE @ver VARCHAR(7)
>>>SELECT @ver = CASE
>>> WHEN CHARINDEX('6.50', @@VERSION) > 0 THEN '6.50'
>>> WHEN CHARINDEX('7.00', @@VERSION) > 0 THEN '7.00'
>>> WHEN CHARINDEX('8.00', @@VERSION) > 0 THEN '8.00'
>>> ELSE 'Unknown'
>>>END
>>>IF @ver = 'Unknown'
>>>BEGIN
>>>   RAISERROR('Unsupported version of SQL Server.',16,1)
>>>   RETURN -101
>>>END
>>>
>>>DECLARE loop_name INSENSITIVE CURSOR FOR
>>>  SELECT spid
>>>   FROM master..sysprocesses
>>>   WHERE dbid = DB_ID(@dbname)
>>>
>>>OPEN loop_name
>>>DECLARE @conn_id SMALLINT
>>>DECLARE @exec_str VARCHAR(255)
>>>FETCH NEXT FROM loop_name INTO @conn_id
>>>WHILE (@@fetch_status = 0)
>>>  BEGIN
>>>    SELECT @exec_str = 'KILL ' + CONVERT(VARCHAR(7), @conn_id)
>>>    EXEC( @exec_str )
>>>    FETCH NEXT FROM loop_name INTO @conn_id
>>>  END
>>>DEALLOCATE loop_name
>>>
>>>WAITFOR DELAY @delay
>>>GO
>>>
>>>/* Sample Execution:
>>>EXEC sp_dbm_kill_users pubs
>>>EXEC sp_dbm_kill_users @dbname = pubs, @delay = '00:00:05'
>>>*/
>>>
>>>
>>>
>>>
>>>ad wrote:
>>>
>>>>I use the SQL:
>>>>"use master; restore database myBase from disk = 'c:\MyBackup.bak' with
>>>>Replace " in program
>>>>to restore myBase form the disk file MyBackup.bak, the disk file
>>>>MyBackup.bak is a Sql2005 backup file.
>>>>But it result a error:
>>>>Exclusive access could not be obtained because the database is in use.
>>>>
>>>>I think there must have some connection still exist.
>>>>
>>>>How can I disconnect all connections to myBase before execute the
>>>>restore command?
>>>>
>>
Author
22 Nov 2005 5:39 AM
luxspes
ad wrote:
> But I am use Sql2005 Express to develop my application.
> The user can't use these tools.
> How can I disconnect all connections by program?
I am starting to think you need to explain exactly what do you mean
  by "disconnect all connections by program" :S
Show quote
>

AddThis Social Bookmark Button