I've never tried to connect to a different server with tsql. You can try testing this. Set up a table for the server names.
CREATE TABLE [dbo].[ServerName](
[SvrName] [varchar](50) NOT NULL,
CONSTRAINT [PK__Svr] PRIMARY KEY CLUSTERED
(
[SvrName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT [dbo].[ServerName] ([SvrName]) VALUES (N'Server1')
INSERT [dbo].[ServerName] ([SvrName]) VALUES (N'Server2')
INSERT [dbo].[ServerName] ([SvrName]) VALUES (N'Server3')
INSERT [dbo].[ServerName] ([SvrName]) VALUES (N'Server4')
INSERT [dbo].[ServerName] ([SvrName]) VALUES (N'Server5')
GO
The carriage return isn't displaying it as I would like but it should work.
declare @sql1 varchar(max) = ''
declare @sql2 varchar(max) = ''
; with SvrNames as (
SELECT [SvrName]
FROM [DBName].[dbo].[ServerName]
)
select @sql1 = STRING_AGG(' :Connect ' + [SvrName] + ' GO ' + char(10) + char(13) + 'EXECUTE sp_executesql @tsql ' + char(10) + char(13), '')
from SvrNames
select @sql2 = ' DECLARE @tsql nvarchar(500) = ''SELECT value FROM DB.dbo.Tablename''' + @sql1
--select @sql2
EXECUTE (@sql2)
The above script generates and executes the following.
DECLARE @tsql nvarchar(500) = 'SELECT value FROM DB.dbo.Tablename'
:Connect Server1 GO
EXECUTE sp_executesql @tsql
:Connect Server2 GO
EXECUTE sp_executesql @tsql
:Connect Server3 GO
EXECUTE sp_executesql @tsql
:Connect Server4 GO
EXECUTE sp_executesql @tsql
:Connect Server5 GO
EXECUTE sp_executesql @tsql