SQL Server 2025 Error 9328: “%sType specification expected”
Why This Error Happens
Error 9328 is a parser-level error in SQL Server that occurs when the XML or JSON Query/Value engine encounters a malformed type cast or schema declaration within a query—typically inside an XQuery expression, OPENJSON clause, or a CAST/CONVERT statement targeting XML schema collections. This usually stems from a missing or malformed data type token immediately following a cast as, treat as, or instance of keyword in the query text. It can also surface due to a corrupted or incompletely migrated XML Schema Collection after an upgrade from a prior SQL Server version to SQL Server 2025, where metadata referencing the type library did not resolve correctly.
Step-by-Step Resolution Guide
Step 1: Isolate the Offending Query or Stored Procedure
- Check the SQL Server Error Log (via SSMS > Management > SQL Server Logs) or Event Viewer on Windows Server 2025 for the full text of Error 9328 to identify which batch, stored procedure, or XML index rebuild triggered it.
- Run
SELECT * FROM sys.messages WHERE message_id = 9328;to confirm the exact language template and cross-reference it against your application’s query logs.
Step 2: Inspect and Correct the Type Cast Syntax
- Review the XQuery/XML DML statement for syntax like
cast as xs:ortreat asand verify a valid XML Schema type (e.g.,xs:integer,xs:string) immediately follows the keyword with no missing token. - For
OPENJSONcalls, confirm theWITHclause explicitly defines column data types (e.g.,WITH (Name varchar(100), Age int)) rather than leaving the type declaration empty or truncated. - Example fix: change
CAST(xmlCol.value('(/Root/Item)[1]', '') AS ...)toCAST(xmlCol.value('(/Root/Item)[1]', 'nvarchar(50)') AS nvarchar(50)).
Step 3: Validate and Rebuild XML Schema Collections
- Run
SELECT name, xml_collection_id FROM sys.xml_schema_collections;to list all registered schema collections on the affected database. - If a schema collection appears incomplete or was migrated improperly, drop and re-import it using
CREATE XML SCHEMA COLLECTIONwith the original.xsddefinition file. - Restart the SQL Server 2025 service via
services.mscon Windows Server 2025 (orRestart-Service MSSQLSERVERin PowerShell) after re-registering the schema to clear any cached query plans referencing the old metadata.
Step 4: Update SQL Server 2025 Cumulative Updates
- Open SQL Server Setup or check via
SELECT @@VERSION;to confirm your current build number. - Download and apply the latest Cumulative Update (CU) from the Microsoft Update Catalog, since early SQL Server 2025 builds had known parser edge-cases with nested type specifications in JSON/XML functions.
Alternative or Verification Steps
- After applying the fix, re-run the original query in SSMS with Include Actual Execution Plan enabled; if it compiles and executes without throwing Error 9328, the type specification issue is resolved.
- As an alternative, isolate the problematic expression using
SET PARSEONLY ON;before running the statement—this validates syntax without executing it, helping you confirm if the type token is now correctly recognized before rolling the fix into production.