Share via

SQL query across linked servers

Rontech10111 221 Reputation points
2026-03-17T09:13:39.18+00:00

Hi All,

I have a number of linked servers.

In each server I have the same table Q_TESTS.

I would like to run t-sql script across all the servers, select * from Q_TESTS where Act = 1

How can I achieve this?

Kindly advise

Many thanks

SQL Server | SQL Server Transact-SQL
0 comments No comments

Answer accepted by question author
  1. Erland Sommarskog 133.7K Reputation points MVP Volunteer Moderator
    2026-03-17T22:14:50.7233333+00:00

    Run this and copy and paste the result into a query window and run it. Add filters to the query as needed. The query assumes that the linked server is up so that the Q_TESTS is in the default database.

    SELECT string_agg(convert(nvarchar(max), 'SELECT * FROM OPENQUERY(') + 
                      quotename(name) + 
                      ', ''SELECT * FROM Q_TESTS WHERE Act = 1'')',
                      char(13) + char(10) + 'UNION ALL' + char(13) + char(10))
    FROM   sys.servers
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2026-03-17T09:13:54.4066667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    There are two main approaches, depending on whether the servers are already linked and whether SQL Server Management Studio (SSMS) can be used.

    1. Use linked servers with four-part names (T-SQL only)

    Assuming each remote instance is already defined as a linked server (for example: LSRV1, LSRV2, LSRV3) and each has the same table Q_TESTS in the same database and schema, run a query like:

    SELECT 'LSRV1' AS ServerName, *
    FROM LSRV1.YourDatabase.dbo.Q_TESTS
    WHERE Act = 1
    
    UNION ALL
    
    SELECT 'LSRV2' AS ServerName, *
    FROM LSRV2.YourDatabase.dbo.Q_TESTS
    WHERE Act = 1
    
    UNION ALL
    
    SELECT 'LSRV3' AS ServerName, *
    FROM LSRV3.YourDatabase.dbo.Q_TESTS
    WHERE Act = 1;
    

    Key points from the linked-server model:

    • Each remote table is referenced with a four-part name: <linked_server_name>.<catalog>.<schema>.<object_name>.
    • All linked servers must already be created (for example with sp_addlinkedserver) and working (test with sp_testlinkedserver).
    1. Use Central Management Server / Registered Servers (SSMS GUI)

    If SSMS is available and the servers are registered in a Central Management Server (CMS) or a local server group:

    1. In SSMS, open View > Registered Servers (or press Ctrl+Alt+G).
    2. Under Database Engine, expand Central Management Servers (or Local Server Groups) and locate the group that contains all target servers.
    3. Right-click the server group and select New Query.
    4. In the query window that opens, run:
         SELECT *
         FROM Q_TESTS
         WHERE Act = 1;
      
    5. SSMS executes this statement on every server in the group simultaneously.
    6. By default, results from all servers are merged into a single grid, with an extra column showing the server name.

    Multiserver result options (optional):

    • In SSMS, go to Tools > Options > Query Results > SQL Server > Multiserver Results to:
      • Ensure Add server name to the results is enabled.
      • Choose whether to Merge results into one grid or show separate grids.

    Either approach lets the same SELECT * FROM Q_TESTS WHERE Act = 1 run against all servers; linked servers do it from one central instance via T-SQL, while CMS/Registered Servers does it from SSMS.


    References:

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.