Troubleshooting Guide: Error 5344 – Duplicate Column Ordinal in WITH Schema Clause
Why This Error Happens
Error 5344 occurs when a query using the WITH <schema> clause (commonly in OPENROWSET, OPENJSON, or external table definitions) specifies the same column ordinal position more than once. This typically results from a manually authored or auto-generated schema definition where column indexes were copy-pasted incorrectly or a script loop failed to increment the ordinal counter. It can also surface after a schema migration where column mappings were merged or duplicated without validation.
Step-by-Step Resolution Guide
Step 1: Locate and Inspect the Offending Query
- Open SQL Server Management Studio (SSMS) and pull the exact query from the SQL Server error log or Extended Events session.
- Identify the
WITH (...)clause and list every column definition along with its ordinal value (e.g.,col1 VARCHAR(50) 1,col2 INT 2). - Pay close attention to queries using
OPENJSONwith an explicit schema, as these are the most common source of this error.
Step 2: Audit and Correct Column Ordinal Values
- Manually count the ordinal numbers in sequence starting from 1 (or 0, depending on the data source convention).
- Remove any duplicate ordinal references — each column must have a unique position value.
- If the schema was generated programmatically (e.g., via PowerShell or a dynamic SQL script), inspect the generation script for a broken loop counter, such as a hardcoded index instead of an incrementing variable.
Step 3: Rebuild the Schema Definition Cleanly
- Drop and recreate the
WITHclause definition rather than patching it, to avoid missing a secondary duplicate. - For external tables, run:
DROP EXTERNAL TABLE dbo.YourTableName;
then recreate it with corrected ordinals.
- Save the corrected script and re-run it against a test database before deploying to production.
Step 4: Clear Cached Query Plans (If Error Persists)
- Run
DBCC FREEPROCCACHEto eliminate any cached execution plan referencing the old, broken schema. - Restart the SQL Server service via
services.mscon Windows Server 2025 only if the issue persists after cache clearing.
Alternative or Verification Steps
- Re-execute the corrected query in SSMS and confirm it returns results without Error 5344 appearing in the Messages tab or the SQL Server error log.
- As an alternative, use
sp_describe_first_result_setagainst your query to validate the column ordinal sequence programmatically before deploying it in production code.