在我的sql上使用ROW_NUMBER()

时间:2013-11-08 11:48:17

标签: mysql sql sql-server stored-procedures visual-studio-2005

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

1 个答案:

答案 0 :(得分:1)

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

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

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

SELECT 1 AS Number 
WHERE Number = 1

您获得无效的列名称“Number”。但如果您将其包裹在另一个SELECT中,则可以使用列名称。

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

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