kill all user in a database, very useful
2009-12-08 17:28
351 查看
It's very useful to tackle a locked database, when you decide to restore it.
Declare @tblConnectedUsers Table (
SPID int )
Declare @vcSQLText varchar(200),
@iSPID int
--Get the currently connected users
Insert into @tblConnectedUsers
Select p.spid
from master.dbo.sysprocesses p (nolock)
join master..sysdatabases d (nolock) on p.dbid = d.dbid
Where d.[name] = 'dbname' --> database name here
--Loop though the connected users and kill their connections
While 1 = 1
Begin
Select top 1 @iSPID = SPID
From @tblConnectedUsers
Where SPID > IsNull(@iSPID, 0)
order by SPID asc
-- break when there are no more SPIDs
If @@RowCount = 0
Break
--Build the SQL string
Set @vcSQLText = 'Kill ' + Convert(varchar(10), @iSPID)
Exec( @vcSQLText )
End
Declare @tblConnectedUsers Table (
SPID int )
Declare @vcSQLText varchar(200),
@iSPID int
--Get the currently connected users
Insert into @tblConnectedUsers
Select p.spid
from master.dbo.sysprocesses p (nolock)
join master..sysdatabases d (nolock) on p.dbid = d.dbid
Where d.[name] = 'dbname' --> database name here
--Loop though the connected users and kill their connections
While 1 = 1
Begin
Select top 1 @iSPID = SPID
From @tblConnectedUsers
Where SPID > IsNull(@iSPID, 0)
order by SPID asc
-- break when there are no more SPIDs
If @@RowCount = 0
Break
--Build the SQL string
Set @vcSQLText = 'Kill ' + Convert(varchar(10), @iSPID)
Exec( @vcSQLText )
End
相关文章推荐
- How to Kill All Processes That Have Open Connection in a SQL Server Database[关闭数据库链接 最佳方法] -摘自网络
- Liferay get Organization by using User or get All Organizations in liferay database
- MCITP SQL Server 2005 Database Administration All-in-One Exam Guide
- user,group,or role 'test' already exists in the current database,error 15023
- drop all tables in database by T-SQL in SQLServer
- MySql Query To List All The Tables In Perticular Database
- 34.A user, who is authenticated externally, logs in to a remote machine and connects to the database
- Grant permission to user in database
- A read-only user or a user in a read-only database is not permitted to disable
- create a first user and a first database for him in mysql
- SQL Server 游标运用:查看所有数据库所有表大小信息(Sizes of All Tables in All Database)
- SQL2000: Error 15023: User or role '%s' already exists in the current database
- Kill All Active Connections To A Database.
- ORA-02070: database SALES does not support operator USERENV in this context
- SQL Server 游标运用:查看一个数据库所有表大小信息(Sizes of All Tables in a Database)
- [置顶] All about control file in Oracle Database
- DB太大?一键帮你收缩所有DB文件大小(Shrink Files for All Databases in SQL Server)
- Find Text in all columns of all tables in a Database
- 解决SQL Server数据库的"User, group, or role already exists in the current database"问题
- HIVE A read-only user or a user in a read-only database is not permitted to disable read-only mode