Why This Error Happens
Error 2311 occurs when SQL Server attempts to parse or register an XML Schema Collection that contains a duplicate attribute declaration within the same complex type or scope. This typically happens when an XSD file is manually edited, merged from multiple sources, or generated by a third-party tool that duplicates attribute nodes during export. SQL Server’s XML parser strictly enforces W3C schema rules, so any redundant <xs:attribute> definition inside the same <xs:complexType> or <xs:attributeGroup> will cause the schema registration (via CREATE XML SCHEMA COLLECTION or ALTER XML SCHEMA COLLECTION) to fail immediately.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Schema Collection
- Check the SQL Server error log or SSMS output for the exact schema collection name referenced in the error message (the
%lsplaceholders will show the attribute name and containing type). - Run the following query to locate schema collections in the affected database:
SELECT name, schema_id, create_date
FROM sys.xml_schema_collections
WHERE name NOT IN ('sys', 'INFORMATION_SCHEMA');
- Export the raw XSD content using
xml_schema_namespacefunction or extract it from the original deployment script/DACPAC.
Step 2: Locate and Remove the Duplicate Attribute
- Open the XSD file in a text editor or XML-aware IDE (Visual Studio, VS Code with XML extension).
- Search for the specific attribute name mentioned in the error (e.g.,
id,version,lang) within the named complex type. - Delete the redundant
<xs:attribute name="...">declaration, keeping only one instance per complex type/attribute group. - Validate the corrected XSD locally using a standalone validator (e.g.,
xmllint --noout --schema file.xsd) before attempting to re-import it.
Step 3: Re-register the Corrected Schema Collection
- Drop the broken schema collection if it exists in a partially registered state:
DROP XML SCHEMA COLLECTION dbo.YourSchemaCollectionName;
- Re-create it using the cleaned XSD file:
CREATE XML SCHEMA COLLECTION dbo.YourSchemaCollectionName AS
N'<cleaned XSD content here>';
- If deploying via DACPAC or a Database Project in Visual Studio 2022/2025, rebuild the project after saving the corrected
.xsdfile to ensure the duplicate isn’t reintroduced from cached build artifacts.
Step 4: Clear Cached Build Artifacts (If Using CI/CD or SSDT)
- Delete the
binandobjfolders in your SQL Server Database Project (SSDT) to remove stale XSD copies. - Re-run the build/publish pipeline to regenerate the deployment script from the corrected source file.
Alternative or Verification Steps
- Confirm the fix by querying
sys.xml_schema_collectionsagain and running a testINSERTorCASToperation against an XML column that references the schema, ensuring no errors are thrown. - If the schema is deployed via a third-party application (e.g., SharePoint, BizTalk, or a vendor ERP system), check for a newer vendor-supplied patch, as duplicate attributes are often introduced by outdated export templates rather than manual edits.