Query to Kill sessions running a particular query text

  • Hi

    Anyone got a query that will find sessions running a particular piece of SQL text ? and bring back output like Kill <Session ID>;

    Half way there in terms of finding the session,  I just need to include where the sql text like "%select blah blah blah%

    SELECT 'KILL ' + CONVERT(VARCHAR(11), session_id) + N';'

    FROM sys.dm_exec_sessions

    WHERE login_name = N'JOE_BLOGGS';

    thanks in advance

     

  • You could do something like and filter as needed,

     

    SELECT * FROM sys.dm_exec_requests er
    INNER JOIN sys.dm_exec_sessions ses on er.session_id = ses.session_id
    CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) sql_text
  • I need to select all SIDs of a particular user who is running a particular piece of code, there in lies the challenge 😉

  • And produce a kill script from that select.

  • caz100 wrote:

    And produce a kill script from that select.

     

    That query should have the login and the sql command in it, you'll just need to add the filter.

  • caz100 wrote:

    I just need to include where the sql text like "%select blah blah blah%

    I'm curious... WHY?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Because someone who shouldn't have had rights to execute his own SQL statements instead of being limited to existing, properly written stored procedures etc?

Viewing 7 posts - 1 through 6 (of 6 total)

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