从SQL Server查询返回n行数

时间:2012-10-18 16:21:10

标签: sql-server-2008

  

可能重复:
  Row Offset in SQL Server

我有一个类似条件的查询,它返回大约1500行。现在我想知道如何逐行获取行,例如返回75行,然后再返回75行,直到它到达行的末尾。我知道我可以使用像SELECT TOP 75

这样的东西

我尝试了类似这样的东西,但它没有返回任何行

    SELECT * 
    FROM 
       (SELECT ROW_NUMBER() OVER (ORDER BY WarehouseSubType.id) as row,
               WarehouseSubType.id, 
               WarehouseType.name as WarehouseTypename,
               WarehouseType.alternateName AS WarehouseTypealternateName,
               WarehouseSubType.name AS WarehouseSubTypename,
               Warehouse.alternateName AS WarehousealternateName,
               WarehouseSubType.alternateName AS WarehouseSubTypealternateName,
               WarehouseSubType1.name AS WarehouseSubType1name,
               WarehouseSubType1.alternateName AS WarehouseSubType1alternateName,
               Warehouse.alternateName AS Warehousename,
               Branch.name AS Branchname,
               Branch.alternateName AS BranchalternateName,
               WarehouseProductQuantity.actualQuantity,
               WarehouseProductQuantity.reservedQuantity,
               Supplier.companyName, 
               Supplier.companyNameAlternate,
               Tafsil.description,
               Tafsil.alternateDescription,
                (WarehouseProductQuantity.actualQuantity - WarehouseProductQuantity.reservedQuantity) AS quantity 
        FROM WarehouseSubType 
        INNER JOIN WarehouseType ON (WarehouseSubType.warehouseTypeId = WarehouseType.id)
        INNER JOIN WarehouseSubType1 ON (WarehouseSubType.id = WarehouseSubType1.warehouseSubTypeId)
   ) a 
WHERE 
    warehouseTypename like '%Ve%' 
    AND row > 0 and row < 75

1 个答案:

答案 0 :(得分:1)

您发布的代码甚至不应该编译,更不用说返回行了。子查询未关闭,内部查询中无法识别行。

请改为尝试:

SELECT * FROM 
   ( SELECT ROW_NUMBER() OVER (ORDER BY WarehouseSubType.id) as row,
    WarehouseSubType.id, 
    WarehouseType.name as WarehouseTypename,
    WarehouseType.alternateName AS WarehouseTypealternateName,
    WarehouseSubType.name AS WarehouseSubTypename,
    Warehouse.alternateName AS WarehousealternateName,
    WarehouseSubType.alternateName AS WarehouseSubTypealternateName,
    WarehouseSubType1.name AS WarehouseSubType1name,
    WarehouseSubType1.alternateName AS WarehouseSubType1alternateName,
    Warehouse.alternateName AS Warehousename,
    Branch.name AS Branchname,
    Branch.alternateName AS BranchalternateName,
    WarehouseProductQuantity.actualQuantity,
    WarehouseProductQuantity.reservedQuantity,
    Supplier.companyName, 
    Supplier.companyNameAlternate,
    Tafsil.description,
    Tafsil.alternateDescription,
    (WarehouseProductQuantity.actualQuantity - WarehouseProductQuantity.reservedQuantity) AS quantity 
    FROM WarehouseSubType 
    INNER JOIN WarehouseType 
        ON ( WarehouseSubType.warehouseTypeId = WarehouseType.id)
    INNER JOIN WarehouseSubType1 
        ON (WarehouseSubType.id = WarehouseSubType1.warehouseSubTypeId)) a 
    WHERE warehouseTypename like '%Ve%' ) b
WHERE b.row > 0 and b.row< 75