Problem killing SPID with script

  • Hello,

    I'm running the following script in Query Analyzer to locate locks that last for more than a specified time (30 seconds in this script).

    declare @blk smallint

    set @blk = 0

    while @blk =0

    begin

    select @blk = blocked

    from sysprocesses

    where blocked >0

    if @blk is null

     set @blk = 0

    else

     waitfor delay '000:00:30' 

     select @blk = blocked

     from sysprocesses

     where blocked >0

     if @blk is null

      set @blk = 0

    end

    kill @blk

    The problem is, when I include the statement kill @blk (last line) I receive the following error message:

    Server: Msg 170, Level 15, State 1, Line 18

    Line 18: Incorrect syntax near '@blk'.

    Can anyone tell me if it's possible to pass a variable value to the kill statement and, if so, how?

    Thanks.

    Paul

     

  • you cannot use kill with a variable. one way to overcome this problem is using dynamic sql.

  • exec ('kill '+cast(@blk as varchar))

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply