在不同 SQL 服务器中终止会话

Kill Session in Different SQL Server

我已进行 SQL 查询以终止所有阻塞会话

declare @tbl table (sesid int)
insert into @tbl 
select BlkBy from (SELECT  spid,sp.[status],loginame [Login],hostname, blocked BlkBy,sd.name DBName, 
cmd Command,cpu CPUTime,physical_io DiskIO,last_batch LastBatch,[program_name] ProgramName   
FROM master.dbo.sysprocesses sp JOIN master.dbo.sysdatabases sd ON sp.dbid = sd.dbid ) as tbl
where BlkBy <> 0
DECLARE @n int, @i int= 0, @s int, @killCommand nvarchar(20)= 'kill ', @sql nvarchar (255)
SELECT @n = COUNT(*) FROM @tbl
WHILE @i < @n
BEGIN 
    SELECT TOP 1 @s = sesid from @tbl
    SET @sql = @killCommand + cast(@s as nvarchar(10))
    EXECUTE sp_executesql @sql
    delete from @tbl where sesid = @s
    SET @i = @i + 1
END

现在我正在开发一个 WEB 应用程序来控制超过 1 个 SQL 服务器,所有这些服务器都链接到控制应用程序数据库,我需要从另一个 [=16] 调用最终的 Kill 命令=] 链接服务器,那么如何终止从一个服务器到另一个链接服务器的会话?

对链接服务器执行直通命令
{ 执行 |执行}
( { @string_variable | [ N ] 'command_string [ ? ]' } [ + ...n ]
[ { , { 值 | @variable [ 输出 ] } } [ ...n ] ]
)
[作为{登录| USER } = '姓名']
[在linked_server_name]
[在DATA_SOURCEdata_source_name]
[;]

MS Docs EXECUTE command