T-SQL Script to kill users of DB
Kill Users of DB
DECLARE @SPID smallint
DECLARE @Loginame varchar(32)
DECLARE @KILLSTATE varchar(32)
-- CREATE TEMPORARY TABLE FOR SP_WHO TO POPULATE
CREATE TABLE #usrkill ( spid smallint,
ecid int,
status varchar(32),
loginame varchar(32),
hostname varchar(32),
blk char(8),
dbname varchar(32),
cmd varchar(255)
)
-- INSERT INTO TEMP TABLE
INSERT #usrkill exec sp_who
-- BEGIN CURSOR
DECLARE KILL_USERS CURSOR FOR
SELECT SPID, Loginame from #usrkill where UPPER(dbname) IN ('MyDB1','MyDB2')
OPEN KILL_USERS
FETCH NEXT FROM KILL_USERS into @spid, @Loginame
IF @@FETCH_STATUS <> 0 GOTO Last_User_Record
WHILE @@FETCH_STATUS = 0
BEGIN
Print 'Killing User: ' + @Loginame
select @KILLSTATE = 'KILL ' + CONVERT(char, @spid)
exec(@KILLSTATE )
FETCH NEXT FROM KILL_USERS into @spid, @Loginame
END
Last_User_Record:
CLOSE KILL_USERS
DEALLOCATE KILL_USERS
-- CLEAN UP TEMPDB
DROP TABLE #usrkill
The above T-SQL statements will all kill the users of the DB MyDB1 and MyDB2. It uses the sp_who proceudre which returns all the users of DB.
0 Comments:
Post a Comment
<< Home