| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> mssql >> 使用T-SQL强制所有用户退出当前数据库 shi yong T-SQL qiang zhi suo you yong hu tui chu dang qian shu ju ku
 

使用T-SQL强制所有用户退出当前数据库

2008-07-18 06:17:31 编辑來源:互联网 繁體版 评论
 
 
本文为【使用T-SQL强制所有用户退出当前数据库】的汉字拼音对照版显示拼音
  mude:qiangzhisuoyouyonghu退tuichudangqianfuwuqi

  miaoshu:gaidaimazhongzhishujukudesuoyouyonghujincheng. yonghujinchengshi sp_who, huozhe sp_who2, SPID > 50 fanhuidesuoyoujincheng. daima使shiyong sp_who (buqiancherenhexitongbiao)chazhaoyunxingzaidangqianshujukushangdejincheng, bingtongguo使shiyong KILL <jinchenghao> zhongzhijincheng..

  yonghukeyizhiding @MaxAttemptsToKillEachUserProcess bianliang(changshizhongzhidangejinchengdecishu, morenzhi 3). ruguochaoguozuidazhi, daimajiangfanhuiyigecuowu (liru, jinchengbunengbeizhongzhi).

  jieguoji: NA

  jieguojipaixu: NA

  使shiyongde TABLES/VIEWS: NA

  REVISIONS

  DATE DEVELOPER DESCRIPTION OF REVISION VERSION

  ========= =============== ================================= ===========

  05/05/2005 Omri Bahat Initial release 1.00

  ==================================================================================

  Copyright ?SQL Farms Solutions, www.sqlfarms.com. All rights reserved.

  This code may be used at no charge as long as this copyright notice is not removed.

  ==================================================================================*/

  DECLARE @MaxAttemptsToKillEachUserProcess INT

  DECLARE @CurrentAttempts INT

  DECLARE @ServerName NVARCHAR(128)

  DECLARE @DatabaseName NVARCHAR(128)

  DECLARE @SQLCommand NVARCHAR(128)

  DECLARE @SPID INT

  DECLARE @LoginName NVARCHAR(128)

  SET NOCOUNT ON

  SET @MaxAttemptsToKillEachUserProcess = 3

  -- dedaofuwuqiheshujukumingcheng

  SET @ServerName = CAST(ISNULL(SERVERPROPERTY('ServerName'), 'Unknown') AS SYSNAME)

  -- gaibiaojiluyonghujinchengbiaoshi.

  IF OBJECT_ID('tempdb..#tblUserProcesses', 'U') IS NOT NULL

  DROP TABLE #tblUserProcesses

  CREATE TABLE #tblUserProcesses (

  SPID INT,

  ECID INT,

  Status NVARCHAR(256),

  LoginName NVARCHAR(128),

  HostName NVARCHAR(128),

  BlockedBy NVARCHAR(128),

  DatabaseName NVARCHAR(128),

  Command NVARCHAR(256))

  INSERT INTO #tblUserProcesses

  EXEC SP_WHO

  DELETE FROM #tblUserProcesses

  WHERE SPID <= 50 OR SPID = @@SPID

  WHILE EXISTS(SELECT * FROM #tblUserProcesses WITH (NOLOCK))

  BEGIN

  SET @SQLCommand = NULL

  SET @SPID = NULL

  SET @LoginName = NULL

  SET @DatabaseName = NULL

  SELECT TOP 1 @SQLCommand = 'KILL ' + CAST(SPID AS NVARCHAR(32)),

  @SPID = SPID,

  @LoginName = ISNULL(LoginName, 'NA'),

  @DatabaseName = DatabaseName

  FROM #tblUserProcesses WITH (NOLOCK)

  SET @CurrentAttempts = 0

  WHILE @CurrentAttempts <= @MaxAttemptsToKillEachUserProcess

  BEGIN

  EXEC(@SQLCommand)

  IF @@ERROR <> 0

  BEGIN

  PRINT(N'Error killing process ' + CAST(@SPID AS VARCHAR(32)) + N', of login ' + @LoginName

  + N', on database ' + @DatabaseName

  + N'. The process was probably terminated by the user.')

  BREAK

  END

  -- qingchubiyaodebiao

  WAITFOR DELAY '00:00:03'

  -- querenjinchengzhenzhengzhongzhi

  INSERT INTO #tblUserProcesses

  EXEC SP_WHO @SPID

  IF @@ROWCOUNT = 0

  BREAK

  ELSE

  SET @CurrentAttempts = @CurrentAttempts + 1

  END

  IF @CurrentAttempts > @MaxAttemptsToKillEachUserProcess

  BEGIN

  PRINT(N'The number of attempts to kill process ' + CAST(@SPID AS VARCHAR(32)) + N', of login ' + @LoginName

  + N', on database ' + @DatabaseName + N' exceeded the maximum number of retry attempts. Script is aborting.')

  RETURN

  END

  DELETE FROM #tblUserProcesses

  INSERT INTO #tblUserProcesses

  EXEC SP_WHO

  DELETE FROM #tblUserProcesses

  WHERE SPID <= 50 OR SPID = @@SPID

  END

  GO原文
 
 
 
 
上一篇《SQL Server 2005 FOR XML嵌套查询使用详解》
下一篇《另类解读SQL Server中的DateTime数据类型》
 
 
 
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
目的:强制所有用户退出当前服务器。 描述:该代码终止数据库的所有用户进程. 用户进程是 sp_who, 或者 sp_who2, SPID > 50 返回的所有进程. 代码使用 sp_who (不牵扯任何系统表)查找运行在当前数据库上的进程, 并通过使用 KILL <进程号> 终止进程.. 用户可以指定 @MaxAttemptsToKillEachUserProcess 变量(尝试终止单个进程的次数, 默认值 3). 如果超过最大值, 代码将返回一个错误 (例如, 进程不能被终止). 结果集: NA 结果集排序: NA 使用的 TABLES/VIEWS: NA REVISIONS DATE DEVELOPER DESCRIPTION OF REVISION VERSION ========= =============== ================================= =========== 05/05/2005 Omri Bahat Initial release 1.00 ================================================================================== Copyright ?SQL Farms Solutions, [url=http://www.sqlfarms.com]www.sqlfarms.com[/url]. All rights reserved. This code may be used at no charge as long as this copyright notice is not removed. ==================================================================================*/ DECLARE @MaxAttemptsToKillEachUserProcess INT DECLARE @CurrentAttempts INT DECLARE @ServerName NVARCHAR(128) DECLARE @DatabaseName NVARCHAR(128) DECLARE @SQLCommand NVARCHAR(128) DECLARE @SPID INT DECLARE @LoginName NVARCHAR(128) SET NOCOUNT ON SET @MaxAttemptsToKillEachUserProcess = 3 -- 得到服务器和数据库名称 SET @ServerName = CAST(ISNULL(SERVERPROPERTY('ServerName'), 'Unknown') AS SYSNAME) -- 该表记录用户进程标识. IF OBJECT_ID('tempdb..#tblUserProcesses', 'U') IS NOT NULL DROP TABLE #tblUserProcesses CREATE TABLE #tblUserProcesses ( SPID INT, ECID INT, Status NVARCHAR(256), LoginName NVARCHAR(128), HostName NVARCHAR(128), BlockedBy NVARCHAR(128), DatabaseName NVARCHAR(128), Command NVARCHAR(256)) INSERT INTO #tblUserProcesses EXEC SP_WHO DELETE FROM #tblUserProcesses WHERE SPID <= 50 OR SPID = @@SPID WHILE EXISTS(SELECT * FROM #tblUserProcesses WITH (NOLOCK)) BEGIN SET @SQLCommand = NULL SET @SPID = NULL SET @LoginName = NULL SET @DatabaseName = NULL SELECT TOP 1 @SQLCommand = 'KILL ' + CAST(SPID AS NVARCHAR(32)), @SPID = SPID, @LoginName = ISNULL(LoginName, 'NA'), @DatabaseName = DatabaseName FROM #tblUserProcesses WITH (NOLOCK) SET @CurrentAttempts = 0 WHILE @CurrentAttempts <= @MaxAttemptsToKillEachUserProcess BEGIN EXEC(@SQLCommand) IF @@ERROR <> 0 BEGIN PRINT(N'Error killing process ' + CAST(@SPID AS VARCHAR(32)) + N', of login ' + @LoginName + N', on database ' + @DatabaseName + N'. The process was probably terminated by the user.') BREAK END -- 清除必要的表 WAITFOR DELAY '00:00:03' -- 确认进程真正终止 INSERT INTO #tblUserProcesses EXEC SP_WHO @SPID IF @@ROWCOUNT = 0 BREAK ELSE SET @CurrentAttempts = @CurrentAttempts + 1 END IF @CurrentAttempts > @MaxAttemptsToKillEachUserProcess BEGIN PRINT(N'The number of attempts to kill process ' + CAST(@SPID AS VARCHAR(32)) + N', of login ' + @LoginName + N', on database ' + @DatabaseName + N' exceeded the maximum number of retry attempts. Script is aborting.') RETURN END DELETE FROM #tblUserProcesses INSERT INTO #tblUserProcesses EXEC SP_WHO DELETE FROM #tblUserProcesses WHERE SPID <= 50 OR SPID = @@SPID END GO
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
清纯漂亮的美女
甜美的佳人
都市丽人写真
郊外美女 春色怡人
夕阳无限好--明天有早晨
雪,还是雪
hk夏日
日暮陆家嘴
 
>>返回首页<<
 为你推荐
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
©2005- 王朝网络 版权所有