Fix SQL Server Error 4104 After Wrapping a Query for Paging

“The multi-part identifier could not be bound” after adding a paging wrapper means the outer query references a table alias that exists only inside the derived table. Project the needed columns from the inner query, then use the derived-table alias outside it.

Last updated: November 1, 2026.

SELECT page.PostId, page.Title, page.CreatedAt
FROM (
    SELECT p.PostId, p.Title, p.CreatedAt, p.IsDeleted
    FROM dbo.Posts AS p
    WHERE p.TenantId = @TenantId
) AS page
WHERE page.IsDeleted = 0
ORDER BY page.CreatedAt DESC, page.PostId DESC
OFFSET @Skip ROWS FETCH NEXT @Take ROWS ONLY;

The outer scope knows only page and the columns selected by the inner query. It cannot reference p.IsDeleted because alias p ends at the closing parenthesis.

Keep each alias inside its scope

Microsoft’s documentation for SQL Server error 4104 explains that a qualifier must correspond to a table or alias available in the statement. Once a table receives an alias, use that alias rather than the physical table name in the same scope.

If a filter belongs logically inside the caller query, apply it there using the caller’s alias. If the paging component must apply it outside, make the column part of the derived table’s public projection and qualify it with the wrapper alias.

Build paging SQL from a defined contract

Avoid rewriting arbitrary SQL text with string replacement. Define which projected columns the wrapper requires, validate sort choices against an allowlist, and keep values such as @Skip and @Take parameterized. Identifiers cannot be supplied as value parameters.

Use a deterministic order with a unique tie-breaker so rows do not move unpredictably between pages. Run the inner query alone first, then the wrapper without optional filters, and add one outer predicate at a time. This quickly reveals which qualifier belongs to a hidden scope. Also inspect generated SQL and parameters together rather than debugging only the original template.

Continue with understanding parameter boundaries, structuring complex SQL Server filters, and checking resulting plans in Query Store.

Related Web Cheat Sheet guides

Sergey Kornilov

Sergey Kornilov