Troubleshooting Guide: SQL Server Error 458 – XML Schema Collection Cross-Database Reference
Why This Error Happens
This error occurs because a SELECT INTO statement is attempting to create a new table with an XML column that is bound to an XML Schema Collection (XSC) residing in a different database than the destination table. SQL Server 2025 enforces strict database-scoping rules for XML Schema Collections, meaning a schema collection registered in DatabaseA cannot be referenced by a table object being created in DatabaseB. This typically happens during data migration scripts, cross-database ETL jobs, or when copying tables via SELECT * INTO without accounting for the schema-bound XML typing.
Step-by-Step Resolution Guide
Step 1: Identify the Source Schema Collection
- Run the following query in the source database to identify the exact schema collection name and its owning database:
SELECT xsc.name, s.name AS SchemaOwner, DB_NAME() AS SourceDB
FROM sys.xml_schema_collections xsc
JOIN sys.schemas s ON xsc.schema_id = s.schema_id
WHERE xsc.name = 'YourSchemaCollectionName';
- Confirm that the target database (where you are running the
SELECT INTO) does not have this same schema collection registered.
Step 2: Script and Migrate the Schema Collection
- In SQL Server Management Studio (SSMS) 21, navigate to the source database:
Programmability > Types > XML Schema Collections. - Right-click the specific schema collection and select Script XML Schema Collection as > CREATE To > New Query Editor Window.
- Execute this script against the target database to register an identical copy of the schema collection locally.
Step 3: Modify the Table Creation Query
- Since
SELECT INTOcannot dynamically bind to a schema in another DB, you must pre-create the table structure manually instead of relying onSELECT INTOto infer the schema. - Use a
CREATE TABLEstatement first, explicitly casting the XML column to the schema collection now present in the local database:
CREATE TABLE dbo.NewTargetTable (
ID INT,
XMLData XML (CONTENT [dbo].[YourSchemaCollectionName])
);
- Follow this with an
INSERT INTO ... SELECTcommand rather than aSELECT INTOto populate the data.
Step 4: Verify Database Compatibility Level
- Check that the target database compatibility level is set to 170 (or the 2025 equivalent) to ensure XML handling behaves consistently with the source.
- Run:
ALTER DATABASE [YourTargetDB] SET COMPATIBILITY_LEVEL = 170;
Alternative or Verification Steps
- Verification: Re-run the original
INSERT INTO(post-migration) query. If the table populates without Error 458, the schema collection binding was successfully localized. - Alternative Fix: If schema replication is not desired, alter the
SELECTstatement to explicitly cast the XML column to untyped XML usingCAST(XMLColumn AS XML), which strips the schema collection reference and allows theSELECT INTOto succeed without needing the schema locally (note: this results in loss of schema validation on the new table).