Answer the question
In order to leave comments, you need to log in
How to get data by SQL query from multiple linked servers?
There are about 40 SQL servers in the network, all of them are linked to one server. The servers differ only in the data set, the database structure is the same, the password for SA is the same for everyone.
It is necessary to collect data from all servers using an SQL script. An example script is below.
SELECT [Cashes].[CashName],
CashDevices.[DeviceModel]
,[Cashes].Model
,[Cashes].[RegistryNumber]
,[Cashes].[DataChange]
FROM [SES].[dbo].[CashDevices]
inner join [SES].[dbo].[Cashes] Cashes ON [Cashes].ID=[CashDevices].[ID_Cash]
WHERE [DeviceType]='Фиск.память'
Answer the question
In order to leave comments, you need to log in
INSERT INTO "MainTable"
("ServerName", "CashName", "DeviceModel", "Model", "RegistryNumber", "DataChange")
SELECT
-- Имя сервера-источника, чтобы не путаться
'LinkedServer1' as "ServerName"
,[Cashes].[CashName]
,[CashDevices].[DeviceModel]
,[Cashes].Model
,[Cashes].[RegistryNumber]
,[Cashes].[DataChange]
FROM
-- Указание источника <server>.<database>.<schema>.<table>
[LinkedServer1].[SES].[dbo].[CashDevices]
inner join
[LinkedServer1].[SES].[dbo].[Cashes] Cashes ON [Cashes].ID=[CashDevices].[ID_Cash]
WHERE [DeviceType]='Фиск.память'
INSERT INTO "MainTable"
("ServerName", "CashName", "DeviceModel", "Model", "RegistryNumber", "DataChange")
SELECT "CashView".* FROM [LinkedServer1].[SES].[dbo].[CashView] "CashView"
UNION
SELECT "CashView".* FROM [LinkedServer2].[SES].[dbo].[CashView] "CashView"
UNION
SELECT "CashView".* FROM [LinkedServer3].[SES].[dbo].[CashView] "CashView"
UNION
SELECT "CashView".* FROM [LinkedServer4].[SES].[dbo].[CashView] "CashView"
...
UNION
SELECT "CashView".* FROM [LinkedServer40].[SES].[dbo].[CashView] "CashView"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question