How to fix Error 5351 on Windows Server 2025 / Windows 11

Troubleshooting Guide: Error 5351 – Partial File Cannot Be Provided Together with TABLESAMPLE Clause

Why This Error Happens

This error occurs when a query attempts to use the TABLESAMPLE clause against a table or view that involves partial file access, such as remote block storage, filestream partial reads, or a database that has files residing in a partially restored or partially online state. SQL Server 2025 blocks this combination because TABLESAMPLE relies on randomly accessing data pages across the entire physical structure of the table, which is architecturally incompatible with a partial or streamed file retrieval operation. This typically surfaces when working with database snapshots, Stretch Database remote tables, or files that are still undergoing piecemeal restore operations on Windows Server 2025.

Step-by-Step Resolution Guide

Step 1: Identify the Affected Table and File State

  • Run sp_helpfile or query sys.master_files in SQL Server Management Studio (SSMS) to check the status of the filegroup associated with the table in question.
  • Look specifically for filegroups marked as RECOVERY_PENDING, OFFLINE, or RESTORING.
  • Confirm whether the table referenced in your query resides on a remote data source (e.g., Stretch Database or PolyBase external table), as these inherently use partial file streaming.

Step 2: Complete Any Pending Restore Operations

  • If the database is undergoing a piecemeal restore, finish the restore sequence before attempting sampled queries.
  • Use the following command to check restore status:
RESTORE DATABASE YourDB WITH RECOVERY;
  • Verify all secondary filegroups have been brought fully online using:
ALTER DATABASE YourDB MODIFY FILEGROUP [FG_Name] ONLINE;

Step 3: Remove TABLESAMPLE from Remote or Streamed Data Queries

  • Rewrite the query to eliminate the TABLESAMPLE clause when targeting Stretch Database tables, external tables, or FILESTREAM-partial columns.
  • Replace sampling logic with a TOP (N) ORDER BY NEWID() approach as a functional workaround:
SELECT TOP (1000) * FROM YourTable ORDER BY NEWID();
  • Ensure the query targets a fully local, fully online table if sampling behavior is required for statistical accuracy.

Step 4: Validate File Group and Storage Configuration on Windows Server 2025

  • Open Server Manager > File and Storage Services and confirm that the underlying disk volumes hosting the .mdf/.ndf files are not in a degraded or reconnecting state.
  • Check Windows Event Viewer under Windows Logs > System for any disk or Storage Spaces Direct (S2D) warnings that could cause SQL Server to treat files as “partial.”
  • Restart the SQL Server service via SQL Server Configuration Manager if storage reconnected after being flagged as partial.

Alternative or Verification Steps

  • To verify the fix, re-run the original query with TABLESAMPLE against the table once all filegroups report ONLINE status via sys.database_files; the error should no longer trigger.
  • If the issue persists, use the TOP (N) ORDER BY NEWID() workaround permanently for any object that must remain integrated with Stretch Database, PolyBase, or active piecemeal restore operations.