Fix: Linked Server Error 7416 ("no login-mapping exists") Even When Mapping Exists

Problem

You may encounter the following error when querying a Linked Server:

Msg 7416, Level 16, State 2
Access to the remote server is denied because no login-mapping exists.

This can happen even when you have already configured login mapping using sp_addlinkedsrvlogin.


Root Cause

This error is often misleading.

Linked Server authentication works in two stages:

  1. Provider initialization
  2. Login mapping (sp_addlinkedsrvlogin)

If the provider fails to initialize due to missing identity information, SQL Server never reaches the login mapping stage.

As a result:

  • Mapping exists :check_mark:
  • Connection fails :multiply:
  • Error message is misleading

Solution

Include User ID in the @provstr when creating the linked server.

Correct Example

EXEC sp_addlinkedserver 
    @server     = N'MY_API_SERVICE',
    @srvproduct = N'',
    @provider   = N'MSOLEDBSQL',
    @datasrc    = N'localhost,5000',
    @provstr    = N'Server=localhost,5000;TrustServerCertificate=Yes;User ID=MY_GATEWAY_USER;',
    @catalog    = N'my-data-source-name';

Keep your login mapping:

EXEC sp_addlinkedsrvlogin 
    @rmtsrvname  = N'MY_API_SERVICE',
    @useself     = N'false',
    @locallogin  = NULL,
    @rmtuser     = 'MY_GATEWAY_USER',
    @rmtpassword = 'MY_GATEWAY_PASSWORD';

Why This Works

Component Purpose
User ID in @provstr Allows provider to initialize
sp_addlinkedsrvlogin Supplies credentials for authentication

Both are required in some environments.


When This Issue Occurs

Common scenarios include:

  • Non-sysadmin users
  • Custom providers or gateway-based connections
  • SQL Server 2019 / 2022
  • Using @provstr without credentials
  • OLE DB providers such as SQLNCLI11 or MSOLEDBSQL

About @locallogin = NULL

@locallogin = NULL

This creates a default mapping for all local SQL Server logins.

Use cases:

  • Quick setup
  • Admin/testing environments

Recommendation:

For production systems, define explicit mappings per login.


Test Query

SELECT * 
FROM OPENQUERY([MY_API_SERVICE], 'select * from your_table');

Summary

  • Error 7416 is often caused by provider initialization failure
  • Login mapping alone may not be sufficient
  • Adding User ID to @provstr resolves the issue

Tip

If the Linked Server works for sa but fails for other users, this is likely the cause.


Reference

ZappySys Gateway Setup Guide