How to kill session using tsql

casting, database, sql-server, t-sql

Solution

Try the following, I am using sp_executesql. For more information see http://technet.microsoft.com/en-us/library/ms188001.aspx

declare @counter        int;
declare @session_id     int;

set @counter=0;
select 
    @counter= count(*),
    @session_id=cast(req.session_id as int) from sys.dm_exec_requests req where req.command='DbccSpaceReclaim'group by req.session_id
if(@counter>0)
begin

declare @sql nvarchar(1000)
select @sql = 'kill ' +  cast(@session_id  as varchar(50))
 exec sp_executesql  @sql
end

Problem

Hello I have problem with cast issue . Even the results come from query as string i get Msg 102, Level 15, State 1, Line 21 Incorrect syntax near '@session_id'. then I tried to use cast method but it did not worked . ``` declare @counter int; declare @session_id int; set @counter=0; select @counter= count(*), @session_id=cast(req.session_id as int) from sys.dm_exec_requests req where req.command='DbccSpaceReclaim'group by req.session_id if(@counter>0) begin kill @session_id end ```

Original source

Related problems