sql-server-2008stored-proceduresdata-paging

Page-navigation using stored procedure (can not bind a composite identifier)


I am using a stored procedure to make page-navigation when I view a list of stores. I have only one table Store with columns Name & S_Id.

And here is my query :

    SELECT  Stores.Name
    FROM
       (
         SELECT ROW_NUMBER() OVER (ORDER BY Stores.S_Id) AS rownum ,
            Stores.Name
         FROM Stores    
       )AS ordered
    WHERE ordered.rownum BETWEEN [someValue] AND [someValue] 

But when I try to save my procedure I get an error:

can not bind a composite identifier Stores.Name

I have seen a lot of topics but cant find whats wrong. If I doing it with LINQ I will try something like this :

    (select name from Stores order by S_Id).Skip(n).Take(m) .

Solution

  • Your subquery defines a new name - ordered - for your data - so you need to use that new name instead of stores:

    SELECT  
        ordered.Name    <=== you're selecting from the subquery which is called "ordered" - use that name!!
    FROM
       (SELECT 
           ROW_NUMBER() OVER (ORDER BY Stores.S_Id) AS rownum,
           Stores.Name
        FROM Stores    
       ) AS ordered
    WHERE 
       ordered.rownum BETWEEN [someValue] AND [someValue]