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

Troubleshooting Guide: SQL Server Error 5341 — “WITH schema clause cannot be provided without FORMAT = ‘CSV’ option”

Why This Error Happens

This error occurs when a BULK INSERT or OPENROWSET(BULK...) statement includes a WITH clause defining a schema (such as column definitions, field terminators, or format specifications) while the FORMAT parameter is either missing or set to a value other than 'CSV'. SQL Server 2025 enforces strict validation requiring that any schema-related options passed through the WITH clause are only applicable when the source file is explicitly declared as CSV-formatted. This typically results from a query written for a flat file import where the FORMAT = 'CSV' argument was omitted, mistyped, or left over from a different import method (such as native or fixed-width format).

Step-by-Step Resolution Guide

Step 1: Review the Bulk Import Query Syntax

  • Open the query in SSMS (SQL Server Management Studio) or Azure Data Studio that triggered Error 5341.
  • Locate the BULK INSERT or OPENROWSET(BULK...) statement and inspect the WITH clause.
  • Confirm whether a schema definition (e.g., FORMATFILE, FIELDTERMINATOR, or column list) is present without a corresponding FORMAT value.

Step 2: Add or Correct the FORMAT Parameter

  • Explicitly add FORMAT = 'CSV' inside the WITH clause of your statement.
  • Example corrected syntax:
  BULK INSERT dbo.TargetTable
  FROM 'C:\ImportData\sourcefile.csv'
  WITH (
      FORMAT = 'CSV',
      FIRSTROW = 2,
      FIELDTERMINATOR = ',',
      ROWTERMINATOR = '\n'
  );
  • Ensure the file path (e.g., C:\ImportData\) is accessible by the SQL Server service account on Windows Server 2025.

Step 3: Validate the Source File Format

  • Confirm the source file is genuinely comma-separated and saved with a .csv extension (not .txt or .dat with a renamed extension).
  • Open the file in Notepad or a text editor to verify delimiters match FIELDTERMINATOR settings in your query.
  • If using a FORMATFILE, ensure it was generated using the bcp utility with CSV-compatible settings.

Step 4: Check SQL Server Service Permissions

  • Navigate to SQL Server Configuration Manager on Windows Server 2025 / Windows 11.
  • Confirm the SQL Server service account has read access to the folder containing the import file.
  • Restart the SQL Server service if permission changes were made:
  Restart-Service -Name 'MSSQLSERVER'

Alternative or Verification Steps

  • Re-run the corrected BULK INSERT statement and confirm the import completes without Error 5341; check SQL Server Logs via SSMS (Management > SQL Server Logs) to verify no related warnings appear.
  • If the issue persists, use the OPENROWSET(BULK...) function with the SINGLE_CLOB option instead to manually parse the file content, bypassing the native CSV schema requirement entirely.