Kill Session in Microsoft SQL Server.

DECLARE @ID INT

DECLARE @MSG CHAR(8)

While 1 = 1

BEGIN
    Set RowCount 1

 SELECT
  @id =
  spid
 FROM
  Master..Sysprocesses P,
  Master..Sysdatabases D
 WHERE
  D.Name='DatabaseName'
 AND
  D.dbid = P.dbid
  --and P.blocked<>0 --if you want to kill blocked session.


    IF @@rowcount = 0
    break

    SELECT @msg = 'KILL ' + convert(char(8) ,@ID)
    Print @msg
    EXEC( @msg)
    --break
END

GO

Comments

Popular posts from this blog

Agent Installation on Windows Server. SQL Server (Failover Cluster) target addition in OEM 12c

Oracle 10g/11g Linux:SMS the alert logs ORA- errors generated in timestamp

Oracle 11g: Install Instant Client 11.2.0.3.0 on Linux x86_64 Server.