Troubleshooting Guide: Error 5342 — ROWTERMINATOR/FIELDTERMINATOR Requires WITH Schema
Why This Error Happens
Error 5342 occurs when a BULK INSERT or OPENROWSET(BULK...) statement specifies formatting options like ROWTERMINATOR, FIELDTERMINATOR, or DELIMITER outside the required WITH (...) schema clause. SQL Server 2025 parses these options strictly as sub-arguments of the WITH clause, so placing them directly after the source file path or table name without the proper wrapper causes a syntax validation failure. This is almost always a query-authoring issue rather than a server configuration problem, often introduced when scripts are migrated from older T-SQL syntax or copy-pasted from incompatible templates.
Step-by-Step Resolution Guide
Step 1: Locate the Offending Statement
- Check the SQL script or stored procedure referenced in the error output for
BULK INSERTorOPENROWSET(BULK...)calls. - Look specifically for
ROWTERMINATOR,FIELDTERMINATOR, orDELIMITERappearing without a precedingWITHkeyword. - Example of broken syntax:
BULK INSERT dbo.MyTable FROM 'C:\Data\file.csv' ROWTERMINATOR = '\n', FIELDTERMINATOR = ',';
Step 2: Wrap Options Inside the WITH Clause
- Rewrite the statement so all formatting arguments are enclosed in parentheses after
WITH. - Corrected syntax:
BULK INSERT dbo.MyTable FROM 'C:\Data\file.csv' WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n'); - For
OPENROWSET, ensure the same pattern:SELECT * FROM OPENROWSET(BULK 'C:\Data\file.csv', FORMATFILE = 'C:\Data\format.xml') AS DataSource; - Double-check comma placement between arguments inside the parentheses.
Step 3: Validate File Paths and Permissions
- Confirm the file path (e.g.,
C:\Data\file.csv) is accessible from the SQL Server service account on Windows Server 2025. - Verify the SQL Server service account has at least Read permission on the folder via
icacls C:\Data. - If using a UNC path, confirm the SQL Server service account has network share access and that the path is not blocked by Windows Defender Firewall.
Step 4: Re-run and Capture Execution Plan
- Execute the corrected statement in SQL Server Management Studio (SSMS) or Azure Data Studio.
- Enable
SET STATISTICS XML ONbeforehand to confirm the parser accepts the syntax without throwing Error 5342 again. - Review the Messages tab for any secondary warnings related to data type mismatches or format file issues.
Alternative or Verification Steps
- Run
SELECT TOP 10 * FROM dbo.MyTable;after the bulk load completes to confirm rows were inserted correctly with the expected delimiters applied. - If the error persists, try using a Format File (
-fwithbcporFORMATFILE =inOPENROWSET) instead of inline terminators, as this isolates the issue to theWITHclause syntax rather than delimiter logic.