Why This Error Happens
Error 5345 occurs when you define an inline schema using the WITH clause in an OPENROWSET(BULK...) statement and specify only OFFSET or only LENGTH for a column, instead of both. SQL Server 2025’s bulk schema parser requires both the starting byte position (OFFSET) and the exact byte count (LENGTH) to correctly map fixed-width or binary data into a column definition. Without both values, the engine cannot determine the field boundaries, so it rejects the query at parse/compile time rather than risk a silent data-misalignment during the read operation.
Step-by-Step Resolution Guide
Step 1: Review the Inline Schema Definition
- Open the T-SQL script containing the
OPENROWSET(BULK...) WITH (...)clause. - Locate every column definition that uses
OFFSETorLENGTHas part of fixed-width field parsing. - Confirm whether both keywords are present for each column — partial definitions (only one of the two) are the direct trigger for Error 5345.
Step 2: Correct the Column Definitions
- Edit the schema so each affected column explicitly declares both parameters, for example:
SELECT *
FROM OPENROWSET(
BULK 'C:\Data\legacyfile.dat',
SINGLE_BLOB
) AS x
CROSS APPLY OPENJSON(x.BulkColumn)
WITH (
EmployeeID INT '$.id' OFFSET 0 LENGTH 4,
EmployeeName VARCHAR(50) '$.name' OFFSET 4 LENGTH 50
);
- Ensure the
OFFSETvalue reflects the correct starting byte in the source file/stream and theLENGTHvalue matches the exact byte-width expected for that field. - Save the corrected script in your project directory, e.g.,
C:\Scripts\SQL2025\FixOpenRowsetSchema.sql.
Step 3: Validate Syntax in SQL Server Management Studio (SSMS)
- Open SSMS (connected to your SQL Server 2025 instance on Windows Server 2025 or Windows 11).
- Paste the corrected query and use Query > Parse (Ctrl+F5) to confirm there are no syntax errors before executing.
- Execute the query and confirm Error 5345 no longer appears in the Messages tab.
Step 4: Check for Legacy or Copy-Pasted Scripts
- Search your solution or deployment scripts folder (e.g.,
C:\DeploymentScripts\) for other instances ofOPENROWSETorOPENJSONwith inlineWITHschemas. - Apply the same
OFFSET/LENGTHpairing fix to any additional occurrences to prevent the error from resurfacing in other stored procedures or ETL jobs.
Alternative or Verification Steps
- Re-run the corrected query and confirm the result set returns properly parsed columns without truncation or misaligned data — this verifies both the fix and correct byte mapping.
- As an alternative, if you don’t need fixed-width binary parsing, remove
OFFSET/LENGTHentirely and let SQL Server infer schema automatically viaOPENJSONdefault parsing (WITHclause using only JSON path, without byte offsets).