| Lesson 3 | Adding a linked server |
| Objective | Add a linked server with Transact-SQL in SQL Server 2025 and verify remote access after configuring authentication and permissions. |
A linked server gives a SQL Server instance a persistent definition for connecting to another data source. When your application submits a query to Instance A, A can use that definition to retrieve data from Instance B. The definition identifies the provider, destination, and connection settings. Login mappings determine which identity the remote connection uses, while permissions on B determine what that identity can access.
In this lesson, you create a link named RemoteB on Instance A using sp_addlinkedserver. The example destination is sql-b.example.com, and its database is SalesDB. These are illustrative names to replace with your environment's values. The SQL examples describe configuration and verification; they do not represent commands already executed against your servers.
Enterprise Manager belongs to older SQL Server tooling. For SQL Server 2025, administrators can use Transact-SQL or SQL Server Management Studio (SSMS). In SSMS, the graphical entry point is Object Explorer, Server Objects, Linked Servers, New Linked Server. Transact-SQL makes the chosen settings explicit and gives you a script to review and retain with your configuration records.
Connect your query window to Instance A before running the setup statements. A stores the name RemoteB and uses its provider to contact B. The link therefore describes access initiated by A. You do not need to create a reciprocal linked server on B for B to return query results. Configure a separate link on B only if B must independently initiate queries against A.
This distinction matters when troubleshooting. The workstation running SSMS is a client of A; it is not the machine that hosts the provider used by A's linked-server query. A connection that works directly from your workstation does not establish that A can reach B, validate B's certificate, or authenticate to B. Test the actual server-to-server route.
A linked server is also different from an SSMS Registered Servers entry. Registration helps a client organize connections. It does not create a Database Engine linked-server definition. Likewise, a linked server is one remote-access mechanism, not a prerequisite for every possible connection to remote data. Direct client connections and the ad hoc access mechanisms covered later have different configuration requirements.
Before creating the definition, confirm which SQL Server instance owns the required database and how A should address it. Use the correct DNS name, listener, instance information, or port for that deployment. The simple hostname in this lesson assumes the destination is reachable through that name using its configured SQL Server connection settings. Do not copy an example hostname into production unchanged.
Microsoft OLE DB Driver for SQL Server must be installed and available to the Database Engine on A. For SQL Server 2025 linked servers, Microsoft's documentation specifies MSOLEDBSQL with Driver 19 behavior. The example explicitly requests mandatory encryption. B must present a valid certificate whose name matches the connection endpoint and whose certification chain A trusts. Provider installation, network reachability, and certificate configuration are separate prerequisites.
An authorized administrator needs ALTER ANY LINKED SERVER to create the definition. The same permission is required for the server-option and removal procedures used below. These are administrative operations, so the application account that eventually reads Orders does not need to receive broad configuration privileges merely to use an already configured link. sp_addlinkedserver cannot execute inside a user-defined transaction.
Record the proposed alias, endpoint, database, provider, authentication approach, and responsible administrator before making changes. A useful configuration record explains why the connection exists and which application needs it. It also distinguishes example values from environment values, making later reviews easier when certificates, hosts, or application identities change.
Consider a reporting application that connects to A but needs order identifiers maintained on B. Its connection string can continue to address A while a query on A references RemoteB. This arrangement makes A responsible for coordinating the remote request. It does not copy the entire Orders table into A or turn B into a local database. The application still depends on the remote service being available when it requests that data. Decide whether that runtime dependency fits the reporting requirement before choosing a linked server as the access method.
Inspect the catalog on A before running creation code. The following query looks for the exact alias and shows the destination plus two settings that are frequently confused:
SELECT name, provider, data_source, catalog,
is_data_access_enabled, is_rpc_out_enabled
FROM sys.servers
WHERE name = N'RemoteB'
AND is_linked = 1;
If the administrator finds an existing definition, compare its endpoint and settings with the intended configuration. Do not automatically drop and recreate it: other applications, views, jobs, or procedures may already reference that name. An existing link to the wrong destination is a configuration question to resolve, not a reason to run destructive setup code as a shortcut.
Catalog visibility is subject to permissions. An empty result from a restricted account is not conclusive evidence that no link exists. Microsoft's sys.servers documentation describes its visibility rules and the SQL Server 2022-and-later VIEW SERVER SECURITY STATE requirement. Perform this inventory with an account authorized to inspect server configuration.
When the alias is available and the prerequisites are satisfied, run the following on A. Named arguments make the relationship between each value and its purpose visible:
EXEC master.sys.sp_addlinkedserver
@server = N'RemoteB',
@srvproduct = N'',
@provider = N'MSOLEDBSQL',
@datasrc = N'sql-b.example.com',
@provstr = N'Encrypt=Mandatory',
@catalog = N'SalesDB';
| Parameter | Example value | Purpose |
|---|---|---|
@server | RemoteB | The alias stored on A and used as the first part of a remote object name. |
@srvproduct | Empty string | Accompanies the explicitly specified OLE DB provider in this configuration. |
@provider | MSOLEDBSQL | Selects the Microsoft OLE DB provider for this linked-server connection. |
@datasrc | sql-b.example.com | The destination interpreted by the provider, distinct from the local alias. |
@provstr | Encrypt=Mandatory | Passes the encryption requirement to the provider. |
@catalog | SalesDB | The initial database for the remote connection. |
The initial catalog does not create SalesDB or limit the identity to that database by itself. Remote permissions govern access. Similarly, naming the link RemoteB does not rename the destination server. Applications use the alias on A, while the provider resolves the endpoint supplied separately. This separation helps readers understand a four-part query without mistaking the first component for a physical hostname.
Review the six values as a group. A correct hostname paired with an incorrect catalog still produces the wrong configuration. A familiar alias paired with a different endpoint can be more confusing because existing query text may look unchanged. Have the destination's owner confirm the database and access requirements, and keep the reviewed script with the environment record. If you maintain separate development and production instances, confirm both the query window's connection and the destination values before submitting a setup statement. The example names alone do not identify which environment you are currently administering.
The explicit settings follow Microsoft's SQL Server 2025 sp_addlinkedserver reference. Avoid adapting older examples that rely on omitted providers or retired Native Client assumptions. Also avoid copying a provider identifier from an unrelated connection API without checking that API's rules. The linked-server example here intentionally uses MSOLEDBSQL.
Encryption protects the connection. Authentication establishes the remote identity. Authorization determines that identity's permitted actions. Successful configuration of one does not prove the other two. For example, a valid certificate can establish a protected connection while the remote login still fails, and a valid login can connect while lacking SELECT permission on Orders.
Driver 19 changes are particularly relevant when modernizing old scripts. The example chooses mandatory encryption with certificate validation. If validation fails, investigate the certificate name, validity, trust chain, and configured endpoint. Adding a certificate-trust bypass or weakening encryption would change the intended connection policy. Resolve the underlying configuration with the administrator responsible for the destination and its certificates.
Creation adds a default self-mapping for all local logins. Review this behavior before making the link available to applications. Decide which local logins may use it and what should happen for callers without a specific mapping. A linked-server definition is not a grant of unrestricted remote access, but its default mapping is still an administrative choice that needs attention.
A self-mapping attempts to use the caller's security context; an explicit mapping can associate an allowed local login with a remote identity. Windows authentication across multiple hops requires appropriate delegation configuration. Do not assume SQL Server forwards a user's password from an SSMS session. The applicable authentication method and environment determine whether the remote identity can be established.
Keep remote privileges aligned with the actual task. A reporting account that only reads Orders should have the necessary read access, not an administrator identity selected for convenience. The next lesson, adding a remote login mapping, explains the mapping procedure and its parameters. Complete that security configuration before treating the connection as ready for application use.
The data access option controls distributed query access through a linked server. If the earlier catalog inspection shows it disabled and the administrator has approved SELECT access, enable that specific option:
EXEC master.sys.sp_serveroption
@server = N'RemoteB',
@optname = N'data access',
@optvalue = N'true';
Do not confuse this with rpc out, which enables outgoing remote procedure calls. The simple four-part SELECT below does not require RPC OUT. Nor does it require enabling Ad Hoc Distributed Queries. Those settings belong to different operations. Microsoft's sp_serveroption reference describes these distinctions.
Changing a capability flag does not supply a login mapping or remote permissions. Keep the troubleshooting sequence tied to the operation being attempted. For this lesson, the goal is a bounded read from one remote table; remote procedure execution and distributed updates can be evaluated separately when an application actually needs them.
After authentication and permissions are configured, connect to A as the intended caller. Start with a connection test, then request a small result from the specific table:
EXEC master.sys.sp_testlinkedserver
@servername = N'RemoteB';
SELECT TOP (20) OrderID, CustomerID
FROM [RemoteB].[SalesDB].[dbo].[Orders]
ORDER BY OrderID;
This assumes B contains SalesDB.dbo.Orders with the named columns. OrderID is the example ordering key. The SELECT returns at most twenty rows; it does not create test data or guarantee that rows exist. An empty successful result and a failed query are different outcomes. Substitute an approved table and columns if your environment uses another schema.
sp_testlinkedserver raises an exception when its connection test fails. A successful test does not establish permission to read every remote object. The following SELECT checks the operation this caller actually needs. Testing only as an administrator can conceal a missing mapping or permission that affects the application's identity.
Work through failures in order: confirm the destination and reachability from A; check provider availability; investigate encryption or certificate errors; then inspect authentication and remote object permissions. Keep the exact error text for diagnosis. Do not repeatedly recreate the link when the error concerns an existing table permission, and do not change multiple unrelated settings before observing the effect of one correction.
Once the basic read succeeds, keep the first application query equally focused. Request the columns the application needs and apply appropriate predicates instead of starting with an unrestricted SELECT of every remote column. A small returned result is useful for validation, but TOP alone does not prove that a complex distributed query performs little work on B. Before expanding the example into large joins or recurring reports, inspect the execution behavior and test representative workloads. Later lessons compare four-part names and OPENQUERY; successful connection setup is the foundation for that comparison, not evidence that every distributed query will be efficient.
A three-part name such as [SalesDB].[dbo].[Orders] identifies an object in SalesDB on the same SQL Server instance handling the query. It does not use RemoteB. A four-part name, [RemoteB].[SalesDB].[dbo].[Orders], first resolves the linked-server alias on A and then identifies the remote database, schema, and object.
This difference is easy to miss if both instances contain a database named SalesDB. Removing the first component can redirect the query to a local object or produce an object-resolution error. It is not simply an abbreviated spelling of the remote query. Always check which instance your query window is connected to and which instance the object name requests.
Specify the schema explicitly. Although dbo is common, SQL Server does not guarantee that every omitted schema resolves as dbo. Schemas organize objects within a database and are distinct from object ownership. Using the actual schema makes examples clearer and avoids teaching a naming shortcut that can behave differently for another user or database.
When reviewing a query, read the name from left to right and ask four questions: which configured link, which database, which schema, and which object? For a local three-part name, the connection already determines the instance, so only the last three questions remain. This habit helps explain errors without changing the query blindly. It also clarifies why registering another server in SSMS or changing the query window's selected database does not automatically create the RemoteB definition used by a four-part reference.
For optional lab cleanup, first confirm that no application, job, view, or stored procedure still depends on RemoteB. Then an authorized administrator can remove the definition from A, together with its associated linked-login mappings:
EXEC master.sys.sp_dropserver
@server = N'RemoteB',
@droplogins = 'droplogins';
This is a separate removal operation, not the final step of a setup script you want to keep using. It does not delete B's databases, tables, or SQL Server login accounts. The mappings being removed belong to A's linked-server configuration. Without the mapping-removal argument, dropping a definition with associated mappings can fail.
sp_dropserver requires ALTER ANY LINKED SERVER and cannot run inside a user-defined transaction. Retain the reviewed connection configuration and dependency information before an intentional removal so that recovery does not depend on remembering undocumented settings.
You now have the creation sequence: identify the initiating instance, inspect an existing alias, specify the provider and encrypted destination, review the security mapping, and test a limited query as its intended caller. In Lesson 4, configure which local identity can use the link and which remote identity performs the work. That completes the security context needed to use the definition you created here.