Answer:
hi,
Why do you need all the information as XML when your only goal is to get the session ID in order to kill it?
You have no need for XML. Simply declare a variable type INT and insert to it only the session id
pseudo code for example
DECLARE @SESSION_ID INT
SELECT TOP 1 @SESSION_ID = session_id FROM sys.dm_exec_sessions
WHERE ....
KILL @SESSION_ID
You have a great explanation in the official documentation about killing a session in this doc:
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/kill-transact-sql
Please inform us if you need did not succeed and you need some more info, but I think that the document with this short answer cover what you need :-)
Off-topic
Why do you use alias name for the columns in the result SET when the original names are the same?!?
This code make not a lot of sense
session_id as session_id,
login_time as login_time,
login_name as login_name,
host_name as host_name,
program_name as program_name,
client_interface_name as client_interface_name
You need to use alias name when the result SET does not have a name for the column - for example when you use aggregate function like MIN(Column_Name)
, or you use alias name when you have a good reason to change the name which returns from the query - for example you want (for some reason) to get pretty like RonenColumn
instead of an original name like vtwetvuwytovewrtyvuerwytuvh5402h32v53
so in this case you use vtwetvuwytovewrtyvuerwytuvh5402h32v53 as RonenColumn
The upper part of the code has no such need, since you replace the original name with the exact same alias name, so you can simply use the original name
session_id,
login_time,
login_name,
host_name,
program_name,
client_interface_name