基本上我试图对由几个表的联合组成的 sql 查询进行分页。在阅读了有关此主题的几个类似答案后,我的查询如下所示:
SET @PageSelect = '
SELECT CATALOG,
ProductID,
CreateDate,
Brand,
Model,
Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6,
ROW_NUMBER() OVER (ORDER BY CreateDate DESC, ProductID) AS RowNumber
FROM
( SELECT ''Agriculture'' AS CATALOG,
ProductID,
CreateDate,
Brand,
Model,
QCategoryName AS Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
MeterReadoutHours AS ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6
FROM Product1 WITH (NOLOCK)
WHERE (Status = 8)
UNION ALL SELECT ''Cargo-Transport'' AS CATALOG,
ProductID,
CreateDate,
Brand,
Model,
QCategoryName AS Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
MeterReadoutKilometers AS ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6
FROM product6 WITH (NOLOCK)
WHERE (Status = 8)
UNION ALL SELECT ''Construction'' AS CATALOG,
ProductID,
CreateDate,
Brand,
Model,
QCategoryName AS Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
MeterReadoutHours AS ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6
FROM Product2 WITH (NOLOCK)
WHERE (Status = 8)
UNION ALL SELECT ''Forestry'' AS CATALOG,
ProductID,
CreateDate,
Brand,
Model,
QCategoryName AS Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
MeterReadoutHours AS ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6
FROM Product3 WITH (NOLOCK)
WHERE (Status = 8)
UNION ALL SELECT ''Groundscare'' AS CATALOG,
ProductID,
CreateDate,
Brand,
Model,
QCategoryName AS Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
MeterReadoutHours AS ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6
FROM Product4 WITH (NOLOCK)
WHERE (Status = 8)
UNION ALL SELECT ''MaterialHandling'' AS CATALOG,
ProductID,
CreateDate,
Brand,
Model,
QCategoryName AS Category,
YearOfManufacture,
PriceOriginal,
PriceOriginalUnit,
EngineOutput,
Country,
LOCATION,
MeterReadoutHours AS ReadOut,
AttachmentPath1,
AttachmentPath2,
AttachmentPath3,
AttachmentPath4,
AttachmentPath5,
AttachmentPath6
FROM Product5 WITH (NOLOCK)
WHERE (Status = 8)) AS BasicSource
WHERE RowNumber BETWEEN ' + CAST(@inPage * @inPageSize - @inPageSize + 1 AS NVARCHAR) + ' AND ' + CAST(@inPage * @inPageSize AS NVARCHAR) + ' ORDER BY RowNumber'
,但我的行号出现错误:Invalid column name 'RowNumber'.
有人可以解释我做错了什么吗?
我有一个优化问题。我想进行分页(从表格的联合中只取 25->100 个项目)。但在这种特殊情况下......它从联合(〜400k)获得所有结果,这使得它变慢