-
-
Notifications
You must be signed in to change notification settings - Fork 519
Description
When generating a query that uses aliased column names AND paged queries, the ORDER BY syntax uses the aliased column, which SQL Server does not like...
Example Query from Northwind database using aliased column names (see the ORDER BY statement below):
SELECT *
FROM (
SELECT
[XT1].[ProductID] AS [XT1_Products_ProductID],
[XT1].[ProductName] AS [XT1_Products_ProductName],
[XT1].[SupplierID] AS [XT1_Products_SupplierID],
[XT1].[CategoryID] AS [XT1_Products_CategoryID],
[XT1].[QuantityPerUnit] AS [XT1_Products_QuantityPerUnit],
[XT1].[UnitPrice] AS [XT1_Products_UnitPrice],
[XT1].[UnitsInStock] AS [XT1_Products_UnitsInStock],
[XT1].[UnitsOnOrder] AS [XT1_Products_UnitsOnOrder],
[XT1].[ReorderLevel] AS [XT1_Products_ReorderLevel],
[XT1].[Discontinued] AS [XT1_Products_Discontinued],
[XT2].[SupplierID] AS [XT2_Suppliers_SupplierID],
[XT2].[CompanyName] AS [XT2_Suppliers_CompanyName],
[XT3].[CategoryID] AS [XT3_Categories_CategoryID],
[XT3].[CategoryName] AS [XT3_Categories_CategoryName],
ROW_NUMBER() OVER (ORDER BY [XT1_Products_ProductID]) <<- should be [XT1].[ProductID]
AS [row_num] FROM [Products] AS [XT1]
INNER JOIN [Suppliers] AS [XT2] ON [XT1].[SupplierID] = [XT2].[SupplierID]
INNER JOIN [Categories] AS [XT3] ON [XT1].[CategoryID] = [XT3].[CategoryID]) AS [results_wrapper] WHERE [row_num] BETWEEN 16 AND 30
SQL Server does not like the XT1_Products_ProductID column name in the ORDER BY statement.