SQL Server Error 568: How to Fix Unexpected End or Error
In this article
SQL Server Error 568 occurs when the server encounters an unexpected end of data during query execution, often due to corrupted data, network interruptions, or malformed T-SQL statements. This is not a syntax error you can fix with a simple semicolon — it’s a runtime failure indicating the server lost the ability to parse or process the incoming data stream. The fix requires checking the source of the data, validating connections, and ensuring transaction integrity. No third-party tools are needed — this can be resolved using native SQL Server tools and configuration checks.
What You'll Need
- Access to SQL Server Management Studio (SSMS) or another query tool connected to the affected instance
- Administrative privileges on the SQL Server instance
- Network access to the server if the error occurs over a remote connection
- The exact T-SQL query or stored procedure that triggered the error
- Access to SQL Server error logs (via SSMS or file system)
Step-by-Step
- Open SQL Server Management Studio and connect to the instance where the error occurred. Ensure you are logged in with sysadmin or db_owner permissions.
- Locate the exact query or stored procedure that triggered Error 568. If the error appeared in an application, check the application logs for the full SQL statement. If you cannot reproduce it, proceed to step 4 to inspect server logs.
- Run the query in SSMS with the "Include Actual Execution Plan" option enabled (Query > Include Actual Execution Plan). This helps detect malformed XML or binary data being passed as parameters.
- Check the SQL Server error log: Right-click the server name in Object Explorer > Reports > Standard Reports > SQL Server Logs. Look for entries matching the time of the error. Filter for "Error 568" or "unexpected end".
- Examine the query for dynamic SQL that builds strings from user input or external sources. Error 568 often arises when a concatenated string is truncated or contains unescaped control characters like NULL bytes or carriage returns. Use PRINT or SELECT to output the full generated SQL string before execution.
- Verify the data types of all parameters passed to stored procedures. If a VARCHAR parameter receives a NVARCHAR value longer than its declared size, or if a binary stream is cut off mid-packet, the server may fail to parse it. Check the parameter definitions in the procedure’s CREATE script.
- Test the query with simplified data. Replace complex parameters with hardcoded literals. For example, if the query uses a variable @xmlData = (SELECT ... FOR XML PATH), replace it with a known good XML string. If the error disappears, the issue is in the data source.
- Check for network instability. If the error occurs only during remote connections, use SQL Server Configuration Manager to enable TCP/IP and ensure Keep Alive is enabled. Restart the SQL Server service after changes.
- Run DBCC CHECKDB on the affected database to detect physical corruption: EXEC DBCC CHECKDB('YourDatabaseName') WITH NO_INFOMSGS, ALL_ERRORMSGS. If corruption is found, restore from a clean backup.
- If the error persists and originates from a linked server or external data source, test the connection using OPENQUERY with a simple SELECT. If OPENQUERY fails, the issue may be with the OLE DB provider or firewall blocking large data packets.
- Update statistics and rebuild indexes on tables involved in the query: USE YourDatabaseName; EXEC sp_updatestats; ALTER INDEX ALL ON YourTableName REBUILD.
- If the error occurs only during bulk inserts or BCP operations, ensure the data file encoding matches the server’s collation. Use a hex editor to check for unexpected bytes at the end of the file.
Common Mistakes
- Assuming Error 568 is a syntax error and tweaking punctuation — it is a data stream failure, not a missing comma.
- Ignoring the SQL Server error log and only looking at application logs — the root cause is often logged server-side.
- Using NVARCHAR(MAX) for XML parameters without validating content first — malformed XML is a top trigger.
- Not testing queries locally before deploying to production — network latency or client-side truncation may mask the issue until deployment.
- Rebooting the server without checking for corruption — restarting may temporarily mask the error but won’t fix underlying data issues.
Tips
- Always wrap dynamic SQL in TRY...CATCH blocks to capture the full generated statement before it fails.
- Use QUOTENAME() and REPLACE() to sanitize parameter inputs before concatenation — especially when building column names or table names dynamically.
- When working with XML or JSON data, validate it with ISXML() or JSON_VALID() before passing it to SQL Server.
- Enable SQL Server Profiler (or Extended Events) to capture the exact batch sent to the server — this reveals if the client is truncating the data.
- If using Entity Framework or another ORM, ensure the context is not truncating large text fields. Set CommandTimeout to a higher value and check for lazy loading timeouts.
- Regularly run DBCC CHECKDB as part of maintenance plans — corruption that causes Error 568 often begins silently.
Troubleshooting
If the error persists after following the steps above, perform these checks in order:
- Restart the SQL Server service. Sometimes the internal parser state becomes corrupted and a restart clears it. This does not affect data.
- Test the same query from a different client machine. If it works elsewhere, the issue is client-side — check drivers, network adapters, or firewall interference.
- Disable any antivirus or endpoint protection temporarily. Some security software intercepts and modifies SQL traffic, especially over named pipes or TCP.
- Check for Windows updates or SQL Server cumulative updates. Error 568 has been patched in older versions like SQL Server 2016 SP2 and SQL Server 2019 CU12.
- If you suspect the database is corrupted and no backup exists, use DBCC CHECKDB with REPAIR_ALLOW_DATA_LOSS as a last resort — this may delete damaged pages. Always test on a copy first.
- If the error occurs during replication or mirroring, pause the process and validate the distribution database with DBCC CHECKDB. Corrupted transaction logs can cause stream termination.
- If all else fails, script out the database schema and data using SSMS (Tasks > Generate Scripts), create a new database, and reimport. This eliminates hidden corruption in system tables.
Frequently Asked Questions
Is Error 568 caused by insufficient memory?
No — Error 568 is not a memory error. It occurs when the data stream ends prematurely. Low memory may cause timeouts or other errors, but not this specific one.
Can a firewall cause SQL Server Error 568?
Yes — firewalls or intrusion prevention systems that truncate large packets can break the data stream. Test by temporarily disabling the firewall or allowing larger packet sizes.
Does this error mean my database is corrupted?
Possibly — but not always. Run DBCC CHECKDB first. If it returns no errors, the issue is likely in the query or network.
Why does this only happen with stored procedures and not direct queries?
Stored procedures often use dynamic SQL, complex parameter passing, or XML parameters — all common triggers for Error 568. Direct queries are usually static and less prone to parsing issues.
Can I fix this by updating SQL Server?
Updating may help if you're on an older version with a known bug. Check Microsoft’s support site for cumulative updates addressing "unexpected end of data" in your version.
Will restarting the server permanently fix Error 568?
No — if the root cause is corrupted data, malformed input, or network issues, restarting only delays the error. Find and fix the source.