Troubleshooting Guide: SQL Server Error 5138
Why This Error Happens
This error occurs when SQL Server 2025 attempts to create, rename, or reference a database file on cloud storage (such as Azure Blob Storage or an S3-compatible endpoint) using a file name that contains one or more trailing space characters. Cloud storage APIs typically normalize or strip trailing whitespace from object keys, which creates a mismatch between the name SQL Server expects and the name actually stored by the cloud provider. This validation was introduced specifically for cloud-backed file streams because local NTFS volumes tolerate trailing spaces in file names, but remote object storage does not guarantee consistent round-trip behavior for them.
Step-by-Step Resolution Guide
Step 1: Identify the Offending File Name
- Check the full error message text for the exact file name referenced in the ‘%.*ls’ placeholder.
- Run the following query to inspect current database file names and look for unintended trailing characters:
SELECT name, physical_name, LEN(name) AS name_len, LEN(RTRIM(name)) AS trimmed_len
FROM sys.master_files;
- Compare
name_lenandtrimmed_len— a mismatch confirms a trailing space is present.
Step 2: Correct the File Name in the T-SQL Script or Application Code
- Open the script, ETL job, ORM mapping, or application config that generated the
CREATE DATABASE,ALTER DATABASE, orBACKUP/RESTOREstatement. - Remove any trailing whitespace from the logical or physical file name string, especially in concatenated variables (e.g.,
@FileName + ' '). - Use
TRIM()orRTRIM()explicitly when building dynamic SQL file paths:
SET @FileName = RTRIM(@FileName);
- Re-run the operation using the corrected name against the cloud storage container (Azure Blob, S3, etc.).
Step 3: Validate Cloud Storage Credential and Endpoint Configuration
- Confirm the
CREATE CREDENTIALorDATABASE SCOPED CREDENTIALobject used for cloud storage access does not itself contain a padded secret or URL with trailing spaces. - Check
sys.external_data_sources(if using PolyBase/External Tables) or the BACKUP URL syntax for extra spaces copy-pasted from documentation or spreadsheets. - On Windows Server 2025, verify any automation (PowerShell scripts, scheduled tasks, or SQL Agent jobs) that builds file name strings isn’t appending a space via string formatting, such as
"$fileName "in PowerShell.
Step 4: Re-test the Operation
- Execute the original
CREATE DATABASE ... ON (NAME = ..., FILENAME = 'https://<account>.blob.core.windows.net/container/file.mdf')command with the cleaned file name. - Confirm the operation completes without raising Error 5138.
Alternative or Verification Steps
- Re-run
DBCC CHECKDBor querysys.master_files/sys.database_filesagain to confirm all file names report equalLEN()andLEN(RTRIM())values, indicating no trailing spaces remain. - If the error persists despite a clean name, test the same file name against the cloud storage container directly (e.g., using Azure Storage Explorer or AWS CLI
ls) to rule out a provider-side naming restriction or hidden Unicode whitespace character (such as a non-breaking space) thatRTRIM()alone may not remove — in that case useTRANSLATE()orREPLACE()to strip the specific character code.