| |

VerySource

 Forgot password?
 Register
Search
View: 729|Reply: 7

Is there a T-SQL statement to clear the database connection ??? Thank you

[Copy link]

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-16 06:00:01
| Show all posts |Read mode
Sometimes T-SQL statements are used to detach the database, but it is unsuccessful because there are connections in use. Is there a T-SQL statement to clear the database connection ???
Reply

Use magic Report

0

Threads

40

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-6-15 16:30:01
| Show all posts
alter database [database name] set offline with rollback after 10
Reply

Use magic Report

0

Threads

30

Posts

25.00

Credits

Newbie

Rank: 1

Credits
25.00

 China

Post time: 2020-6-15 19:30:01
| Show all posts
- Upstairs is offline
- Suppose you want to clear the database abc connection
declare @spid int
declare @strsql varchar(256)
select @spid=min(spid) from master.dbo.sysprocesses
while @spid is not null
begin
  set @strsql='kill '+ cast(@spid as varchar)
  print(@strsql)
  select @spid=min(spid) from master.dbo.sysprocesses
  where spid>@spid and db_name(dbid) ='abc' and dbid<>0
end
go
Reply

Use magic Report

0

Threads

40

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-6-16 02:45:01
| Show all posts
alter database [database name] set offline with rollback after 10


You can check the online help'alter database'
^_^
Reply

Use magic Report

0

Threads

100

Posts

53.00

Credits

Newbie

Rank: 1

Credits
53.00

 China

Post time: 2020-6-16 08:15:01
| Show all posts
Use kill to kill all connection processes
Reply

Use magic Report

0

Threads

40

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 Brazil

Post time: 2020-6-16 13:30:01
| Show all posts
Oh like this

USE Master
  SET NOCOUNT ON
  Declare @spID varchar(100)
  Declare @SQL varchar(5000)
  Select @spID=``,@SQL=''
   
  Create table #tmp (spid int,ecid int,status varchar(50),loginame varchar(50),hostname varchar(100),blk int,dbname varchar(50),cmd varchar(400))
   
  Insert into #tmp
  Exec sp_who
   
  Select @SQL=@SQL+' Kill'+Cast(spid as varchar) From #tmp where dbname='Northwind'
  exec (@SQL)
  Drop table #tmp
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-6-16 18:15:01
| Show all posts
KILL!
Reply

Use magic Report

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-6-16 22:00:01
| Show all posts
OK, thank you
I used a relatively simple alter database [database name] set offline with rollback after 10
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list