How to fix Error 558 on Windows Server 2025 / Windows 11

Troubleshooting Guide: SQL Server Error 558 — “Remote Function Calls Not Allowed Within a Function”

Why This Error Happens

This error occurs because SQL Server 2025 does not permit calling remote stored procedures or distributed queries from within the context of a user-defined function (UDF), as functions are designed to be deterministic and side-effect-free. The engine blocks these calls to preserve transactional integrity, since remote calls introduce non-deterministic behavior and potential distributed transaction dependencies that functions cannot safely manage. This typically surfaces when legacy T-SQL code or migrated linked-server queries are refactored into scalar or table-valued functions without accounting for this restriction.

Step-by-Step Resolution Guide

Step 1: Identify the Offending Function Code

  • Locate the function throwing Error 558 by checking the SQL Server Error Log or Extended Events session.
  • Run the following query to search definitions for remote calls:
  SELECT OBJECT_NAME(object_id), definition 
  FROM sys.sql_modules 
  WHERE definition LIKE '%OPENQUERY%' OR definition LIKE '%EXEC%'
  • Confirm whether the function references a four-part name (e.g., LinkedServer.Database.Schema.Table) or calls EXEC against a remote procedure.

Step 2: Convert the Function into a Stored Procedure

  • Since remote calls are only allowed in stored procedures or ad-hoc batches, migrate the logic out of the UDF.
  • Recreate the object using CREATE PROCEDURE instead of CREATE FUNCTION:
  CREATE PROCEDURE dbo.usp_GetRemoteData
  AS
  BEGIN
      SELECT * FROM OPENQUERY(LinkedServerName, 'SELECT * FROM RemoteTable')
  END
  • Update any calling application or T-SQL script to use EXEC dbo.usp_GetRemoteData instead of SELECT dbo.OldFunctionName().

Step 3: Refactor Application/Query Logic to Use a Temp Table

  • If the function’s result is used inline within a larger query (e.g., a JOIN), have the calling batch execute the new procedure first and insert results into a temporary table.
  • Example pattern:
  CREATE TABLE #RemoteResults (Col1 INT, Col2 VARCHAR(100));
  INSERT INTO #RemoteResults 
  EXEC dbo.usp_GetRemoteData;

  SELECT * FROM #RemoteResults r
  JOIN LocalTable l ON r.Col1 = l.ID;
  • Ensure the linked server itself (sp_linkedservers) is configured correctly and RPC / RPC OUT options are enabled if remote procedure execution is required:
  EXEC sp_serveroption 'LinkedServerName', 'rpc', 'true';
  EXEC sp_serveroption 'LinkedServerName', 'rpc out', 'true';

Step 4: Restart SQL Server Service to Apply Configuration Changes

  • On Windows Server 2025 or Windows 11, open SQL Server Configuration Manager.
  • Navigate to SQL Server Services, right-click your instance, and select Restart to ensure linked server and RPC settings take effect.

Alternative or Verification Steps

  • Verify the fix by executing the new stored procedure directly (EXEC dbo.usp_GetRemoteData) and confirming data returns without Error 558.
  • If the restriction still applies to reporting or BI tools calling the old function, consider replacing the linked server approach entirely with PolyBase or Linked Server via SSIS staging tables to decouple remote data retrieval from inline function calls.