0

Possible Duplicate:
Row Offset in SQL Server

I have a query which has a like condition and it returns around 1500 rows. Now I want to know how can I get the rows part by part, e.g. return 75 rows, then again the next 75 rows until it reaches the end of the rows. I know I can use something like SELECT TOP 75

I tried something like this but it does not return any rows

    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
5
  • 1
    See this answer: stackoverflow.com/questions/187998/row-offset-in-sql-server Commented Oct 18, 2012 at 16:22
  • @ShawnSteward infact i have used that but does not return any records Commented Oct 18, 2012 at 16:29
  • What version of SQL? If 2012 then there are paging operators Commented Oct 18, 2012 at 16:38
  • @PeteCarter I am using mssql 2008 Commented Oct 18, 2012 at 16:42
  • @ZAJ: if it doesn't return any records, then there isn't any record for your condition... Commented Oct 18, 2012 at 16:49

1 Answer 1

1

The code you posted should not even compile, let alone return rows. The sub query is not closed and row would not be recognised in the inner query.

Try this instead:

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.