| Author |
Topic |
|
cbeganesh
Posting Yak Master
105 Posts |
Posted - 2004-08-19 : 16:03:36
|
| I need to clear the all the db connections before detaching it through t-sql, Does anyone used any commands to clear the connections through t-sqlThanks |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-08-19 : 16:13:46
|
| ALTER DATABASE DBNameGoesHere SET SINGLE_USER WITH ROLLBACK IMMEDIATETara |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-08-19 : 19:16:20
|
| When you need to clear all db connections?Thanksmk_garg |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-08-19 : 19:18:26
|
| mk_garg20, maybe I don't understand the question, but you need to clear all connections when performing a restore or detaching the database.Tara |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2004-08-19 : 21:16:06
|
| Thanks Tara!I never performed any DBA operations. Working as developer.So when i don't understand anything. I prefer to ask you gurus. I started reading on this site last month. I feel i am improving my skills.Thanks to you all.mk_garg |
 |
|
|
cbeganesh
Posting Yak Master
105 Posts |
Posted - 2004-08-20 : 10:57:10
|
| SaraIn which db i have to execute this statement. 'cos i tried running this in master and it ran sucess . But when i try to detach the db, it said "Cannot detach "db" beacuse it is currently being used". But i see no connections open Thanks |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-08-20 : 12:18:45
|
| It's Tara.And you run it from master, but you need to change the database name in the command to the one that you want to detach.ALTER DATABASE ChangeThis SET SINGLE_USER WITH ROLLBACK IMMEDIATETara |
 |
|
|
cbeganesh
Posting Yak Master
105 Posts |
Posted - 2004-08-23 : 15:08:55
|
| Yes TaraI tried running this from master and ran sp_detachdb to detach the db and gives error "cannot detach the db, currently in use"Thanks |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-08-23 : 15:12:00
|
| USE masterGOALTER DATABASE ChangeThis SET SINGLE_USER WITH ROLLBACK IMMEDIATEEXEC sp_detach_db @dbname = 'ChangeThis', @skipchecks = 'true'Did you try it like this (above)?Tara |
 |
|
|
cbeganesh
Posting Yak Master
105 Posts |
Posted - 2004-08-23 : 15:32:45
|
| i usedexec sp_detach_db 'dbname','true'I also tried with your syntax gets the same error not sure why? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-08-23 : 16:18:17
|
| The ROLLBACK IMMEDIATE must not be finished rolling back user connections then. You can run sp_who or sp_who2 to determine who is still connected, then run KILL <spidNo> to get rid of them manually.Tara |
 |
|
|
gpl
Posting Yak Master
195 Posts |
Posted - 2004-08-23 : 18:50:02
|
quote: Originally posted by tduggan The ROLLBACK IMMEDIATE must not be finished rolling back user connections then. You can run sp_who or sp_who2 to determine who is still connected, then run KILL <spidNo> to get rid of them manually.Tara
We needed to do an automated restore on fail of a DTS job, we came up with this procedure--yes I know it uses a cursor, but if its good enough for Microsoft ......Grahamif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Kill_All_spids]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[Kill_All_spids]GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS OFF GOCreate Procedure Kill_All_spids (@WhichDb Varchar(80) = '') As/* created gpl / aj 08/11/2002 This procedure uses code languishing in sp_who2 to produce a cursor of spids of all processes using the named database, except the current one; The cursor is processed and each spid is KILLed*/BeginDeclare @Unwanted_SPID SmallIntDeclare @SQLStr Varchar(10) If @WhichDb = '' GoTo ErrEnd --- crash out if no param Declare All_Unwanted_SPIDS_Cursor Cursor FORWARD_ONLY READ_ONLY For Select spid From master..sysprocesses Where spid != @@spid And Case dbid When 0 Then '' Else db_name(dbid) End = @WhichDb Open All_Unwanted_SPIDS_Cursor Fetch Next From All_Unwanted_SPIDS_Cursor Into @Unwanted_SPID While @@FETCH_STATUS = 0 Begin Set @SQLStr = 'Kill ' + Convert(Varchar(3), @Unwanted_SPID) Exec (@SQLStr)--- now go back for the next one Fetch Next From All_Unwanted_SPIDS_Cursor Into @Unwanted_SPID End --- Loop Close All_Unwanted_SPIDS_Cursor Deallocate All_Unwanted_SPIDS_Cursor Return 0ErrEnd: RaisError ('No Database name provided',16,1) Return -1EndGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO |
 |
|
|
cbeganesh
Posting Yak Master
105 Posts |
Posted - 2004-08-24 : 09:04:57
|
| thanks graham & taraGan |
 |
|
|
|