Troubleshooting Guide: SQL Server Error 6995 – Invalid XML Schema Type Definition
Why This Error Happens
This error occurs because SQL Server’s XML Schema Collection (XSC) implementation explicitly disallows the use of the built-in ID and IDREF XML Schema types (or any custom types derived from them) when defining element types or extending complex types. This restriction exists because ID and IDREF are identity-constraint types reserved for internal XML uniqueness validation, and permitting them as general-purpose element types would conflict with SQL Server’s internal XML indexing and validation engine. The error is almost always triggered by a malformed or improperly migrated .xsd file being registered via CREATE XML SCHEMA COLLECTION or ALTER XML SCHEMA COLLECTION.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Schema Collection
- Run the following query to locate the specific schema collection causing the failure:
SELECT name, schema_id FROM sys.xml_schema_collections WHERE name = '%s'; - Check the SQL Server Error Log (located at
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOGon Windows Server 2025) to capture the exact timestamp and batch that triggered Error 6995. - Open the source
.xsdfile in a text editor or Visual Studio to inspect the type declarations.
Step 2: Remove or Refactor Invalid Type References
- Search the XSD file for any
<xs:element>or<xs:complexType>definitions that usetype="xs:ID"ortype="xs:IDREF". - Replace these references with a safe substitute type, such as
xs:stringorxs:token, which preserves data format without violating SQL Server’s identity-constraint restrictions. - If the schema uses a custom simple type derived by restriction from
ID/IDREF(e.g.,<xs:simpleType name="CustomID"><xs:restriction base="xs:ID">), change thebaseattribute toxs:stringinstead. - Save the corrected
.xsdfile to a working directory, such asC:\SQLSchemas\Corrected\.
Step 3: Re-register the Corrected Schema Collection
- Drop the existing broken schema collection if it was partially created:
DROP XML SCHEMA COLLECTION [SchemaName]; - Re-import the corrected schema using T-SQL:
CREATE XML SCHEMA COLLECTION [SchemaName] AS 'C:\SQLSchemas\Corrected\schema.xsd'; - Confirm the command completes without raising Error 6995 in SQL Server Management Studio (SSMS) or Azure Data Studio.
Step 4: Validate Dependent Objects
- Check any tables or stored procedures referencing the XML Schema Collection using
sp_dependsor thesys.sql_expression_dependenciescatalog view. - Recompile affected stored procedures with
EXEC sp_recompile 'dbo.YourProcedureName';to ensure they bind correctly to the updated schema.
Alternative or Verification Steps
- Verify the fix by inserting a sample XML document that conforms to the corrected schema into a typed XML column and confirming no validation errors are thrown.
- If modifying the
.xsdsource is not feasible (e.g., third-party schema), consider storing the XML as untyped (XMLdata type without a schema collection reference) and enforcing ID/IDREF-like uniqueness constraints at the application layer instead.