0

基本上我试图对由几个表的联合组成的 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)获得所有结果,这使得它变慢

4

1 回答 1

1

SELECT当您在查询的子句中命名列时,AS您不能在WHERE子句中通过该名称引用它。

要引用,ROW_NUMBER()您必须将整个内容包装为子查询。然后在外部查询的WHERE子句中,您可以参考RowNumber.

如果您尝试运行,可以看到相同的错误:

SELECT 1 AS Number 
WHERE Number = 1

您得到无效的列名“数字”。但是,如果将其包装在另一个中SELECT,则可以使用列名。

SELECT Number AS AnotherName FROM (
    SELECT 1 AS Number 
) Numbers
WHERE Number = 1

如果您将WHERE子句更改为WHERE AnotherName = 1,您将再次收到相同的错误,因为查询解析器还不知道您AnotherName在查询中命名了它,它只知道子查询的列名。这是因为当它评估查询时,它会在相关数据WHERE之前根据子句过滤结果集。SELECT

于 2013-11-08T12:01:32.087 回答