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

Troubleshooting Guide: SQL Server Error 6312

Why This Error Happens

This error occurs when SQL Server attempts to validate an XML instance against a schema collection, but the specific target namespace referenced in the XML cannot be located within that collection. This typically happens due to a typo or case-sensitivity mismatch between the namespace URI declared in the XML document and the one registered via CREATE XML SCHEMA COLLECTION. It can also occur if the schema collection was altered or partially dropped, leaving the namespace component orphaned or missing from the internal metadata.

Step-by-Step Resolution Guide

Step 1: Verify the Schema Collection Contents

  • Run SELECT xml_schema_namespace(N'schema_name', N'collection_name'); to dump the actual XSD content currently registered in SQL Server.
  • Check the targetNamespace attribute within the returned XSD text and compare it character-for-character (including case) with the namespace used in your application’s XML payload.

Step 2: Inspect the Source XML for Namespace Mismatches

  • Open the XML document or variable causing the failure and locate the xmlns declaration.
  • Look specifically for trailing slashes (e.g., http://example.com/schema vs. http://example.com/schema/), as SQL Server treats these as distinct namespaces.
  • Ensure the application code (C#/.NET or PowerShell scripts) generating the XML has not been updated to a newer schema version without updating the SQL Server collection.

Step 3: Re-import or Update the Schema Collection

  • If the namespace is genuinely missing, use the ALTER XML SCHEMA COLLECTION command to add the missing components rather than dropping the whole collection (which prevents breaking existing table dependencies).
  • Syntax example: ALTER XML SCHEMA COLLECTION [schema_name].[collection_name] ADD N'<xsd:schema targetNamespace="...">...</xsd:schema>';
  • If using SQL Server Management Studio (SSMS) on Windows Server 2025, ensure you are connected to the correct instance, as collections are database-specific and will not be found if you are querying the wrong database context.

Step 4: Check for Corruption via DBCC

  • If the schema collection appears correct but the error persists intermittently, run DBCC CHECKDB on the specific database to rule out metadata corruption affecting the sys.xml_schema_collections system view.
  • Restart the SQL Server service via services.msc or Restart-Service MSSQLSERVER in PowerShell to clear any cached execution plans that might be referencing a stale version of the schema.

Alternative or Verification Steps

  • Verification: Execute a test INSERT or CAST operation using the exact XML payload that previously failed; if it processes without throwing Error 6312, the namespace mapping is resolved.
  • Alternative: If the schema collection is too complex to patch via ALTER, export the current data to a flat table, DROP the XML Schema Collection entirely, recreate it with the correct, verified targetNamespace, and then re-apply the XML constraint to the table.