How to Fix SQL Server Error 6602 (Msg 6602)
You see sql server error 6602 when the database engine fails to parse an XML document. You fix it by reading the detailed message text and repairing the bad XML input. This failure returns message code 6602 with a severity level of 16. Severity 16 means the user or app made a mistake that the team can fix. The engine stops the current command and prints a short text string about the XML issue. It does not crash the server or corrupt your stored tables. What causes sql server error 6602 in the
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You see sql server error 6602 when the database engine fails to parse an XML document. You fix it by reading the detailed message text and repairing the bad XML input.
This failure returns message code 6602 with a severity level of 16. Severity 16 means the user or app made a mistake that the team can fix. The engine stops the current command and prints a short text string about the XML issue. It does not crash the server or corrupt your stored tables.
What causes sql server error 6602 in the database engine
In the database engine, sql server error 6602 acts as a wrapper for XML parser faults. The official message states that the error description is a specific text string from the parser. When an app sends XML to a system procedure, the engine calls its internal XML parser. If the parser finds broken tags or bad text encoding, it stops work right away. It then sends the parser reason back to the client inside message 6602.
Several common mistakes trigger this message during daily work. An app might send an XML string with an unclosed tag or a missing quote. The payload might declare one text encoding in the header while using different bytes in the body. Sometimes a query passes an empty string or a null value to an XML preparation routine. The parser rejects these bad inputs and raises the severity 16 alert to the client session.
Memory limits or unclosed document handles can also cause problems in busy systems. When a session prepares an XML document, it holds that document in memory until removed. If a script fails to clean up old handles, the session wastes server memory. Always pair your XML preparation steps with a clean removal step in the same session.
How to find and fix the failure in production
Start by reading the exact text string returned inside the 6602 message. That text tells you the exact line and character where the XML parser stopped. Ask the app team to log the raw payload before it reaches the database. Compare the failing payload against a known good sample to spot missing tags or bad characters. Once the app sends valid and well-formed XML, the command will run without errors.
When engineers check production data to find bad XML rows, keep permissions tight. Do not grant full owner rights just to inspect a failing table or view. A database user is the identity of a login when it connects to a database. You can use a SQL user with a login in master for access across many databases. For one or a few databases, an admin can enable contained databases and create a contained user.
Kubernetes API Server Access + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Assign the lowest fixed role needed to inspect the broken records safely. Members of the datareader role can read all data from all user tables and views. Never use the owner role for simple checks, because it can drop the database. Note that you cannot change the permissions assigned to fixed database roles. If sensitive fields like emails, phone numbers, card numbers, or national IDs exist, masking rewrites matched fields in results.
How to prevent XML parsing errors in your apps
You can stop message 6602 before it happens by checking XML in the app layer. Set up your app code to check every XML file against a strict schema first. Reject bad files early so they never reach the database engine. This simple step saves database compute time and keeps production logs clean.
You should also test how your database scripts handle broken inputs. Wrap your XML processing calls in error handling blocks inside your stored procedures. If a call fails, make sure the script still closes any open XML document handles. Good cleanup habits keep your database sessions fast and stable under heavy load.
Add runtime guardrails beside SQL Server
Role grants and network rules are needed setup for SQL Server. Still, an allowed user or agent tool can send a bad delete or read raw customer fields over a valid session. In practice, hoop.dev sits inline in the TDS protocol beside the database as a sidecar for your SQL Server. For every query an agent or human runs in production, hoop.dev decides whether to pass, mask, block, or escalate it for review.
With hoop.dev SQL Server controls, rules refuse banned statements before they run, hold risky queries for human review, and mask matched sensitive values in supported result columns in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for HIPAA, PCI DSS, SOX, and GDPR audits. See the source on GitHub.
Frequently asked questions
What does severity 16 mean for message 6602?
Severity 16 means the error comes from bad user input or broken query syntax. It does not point to a hardware crash or a damaged database file. Your team can fix it by correcting the XML data or the query call.
Why does the error message show a placeholder string?
In the official catalog, message 6602 uses a placeholder for the parser text. When the error happens in real life, the engine fills that spot with the real reason. You will see details like an unclosed tag name or an invalid character code.
Which database role should an engineer use to check failing XML rows?
Give the engineer the fixed datareader role on the target database. This role lets them read all user tables and views to find the bad XML text. It prevents them from changing data or dropping tables by mistake during the check.
Related guides
In this post