Troubleshooting Guide: SQL Server Error 6826 – “Every IDREFS or NMTOKENS Column Must Appear in a Separate SELECT Clause”
Why This Error Happens
This error occurs when a FOR XML EXPLICIT query attempts to combine multiple columns typed as IDREFS or NMTOKENS within the same SELECT statement branch, which violates the XML data type requirements for multi-value attribute columns. The SQL Server XML parser requires each IDREFS or NMTOKENS column to be isolated in its own SELECT clause because these data types represent space-separated token lists that cannot be merged or interleaved with other columns at the same tag/parent level. This is not a permissions or connectivity issue, but rather a syntax structure violation specific to how FOR XML EXPLICIT mode constructs hierarchical XML output.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Query and Columns
- Open SQL Server Management Studio (SSMS) 2025 and locate the exact query referenced in the error stack trace or application log.
- Review the column aliasing syntax, specifically looking for columns formatted as
TagName!TagNumber!AttributeName!IDREFSor!NMTOKENS. - Check if two or more of these typed columns exist under the same
TagNumbergrouping in a single SELECT statement.
Step 2: Separate the Columns into Distinct SELECT Clauses
- Rewrite the query so that each
IDREFSorNMTOKENScolumn resides in its own standalone SELECT block. - Use
UNION ALLto join these separated SELECT statements back together, ensuring theTagNumberandParentcolumns remain consistent across all unioned blocks. - Example fix pattern: Convert
SELECT Tag, Parent, Col1!1!Attr1!IDREFS, Col2!1!Attr2!IDREFS FROM...into two separate SELECT statements joined byUNION ALL, one forCol1and one forCol2.
Step 3: Validate Tag and Parent Consistency
- Ensure the
TagandParentinteger values match exactly across all newly separated SELECT clauses so the XML hierarchy reconstructs correctly. - Confirm that NULL placeholders are inserted for non-relevant columns in each separated clause to maintain column count parity.
- Re-run the query in a test environment (non-production database) before deploying to Windows Server 2025 production instances.
Step 4: Consider Migrating to FOR XML PATH or AUTO Mode
- If the
EXPLICITmode logic has become overly complex, evaluate refactoring the query to useFOR XML PATH('')instead, which does not impose the same single-column restriction for token-list types. - Test output compatibility with downstream consumers (APIs, BizTalk, or legacy parsers) since
PATHmode generates slightly different XML formatting thanEXPLICIT.
Step 5: Apply Latest Cumulative Update
- Verify you are running the latest Cumulative Update (CU) for SQL Server 2025 via
SELECT @@VERSION;in a new query window. - Download and install any pending CU from the Microsoft Update Catalog if the build number is outdated, as XML parsing edge-case fixes are occasionally included in servicing releases.
Alternative or Verification Steps
- Execute the corrected query using
SET STATISTICS XML ONor simply run it standalone in SSMS to confirm the result set returns valid, well-formed XML without triggering Error 6826. - If the error persists after restructuring, temporarily substitute
IDREFS/NMTOKENStyped columns with standardstringtype output and manually format the space-separated values usingSTRING_AGG()as a workaround.