Leron
Leron

Reputation: 9886

Implementing pagination in working query

I am connecting to a SQL SERVER 2012 with 3 linked servers on it. I'm using premade queries to fetch data, but the person who made the queries did not implemented pagination which I'm gonna need so I'm trying to impelment this featuer. One thing to notice is that I don't know the version of the linked servers. It's may very first time using linked servers so I'm not familiar with the way they works but since on my machine I have 2012 version I tried using OFFSET and FETCH but got erros for invalid token. Then I was suggested to try the syntax that worked on my local SQL SERVER 2008R2 but I can't do this too so, here it is. First the code that is working :

select *
from openquery(linkedServer,
'
select 
  ORG.Id as ID,  
  ORG.FirstName AS "FirstName",
  ORG.LastName as LastName

from YMFK.LK99Z ORG 

left outer join YMFK.LK88Z Phone
  ON ORG.Number = Phone.Number
    AND Phone.Col = ''something'' AND Phone.Col = ''else''
inner join YMFK.L77Z ORL
    ON ORG.Id = ORL.Id 
where 
ORG.FirsName  like ''%Peter%'''
)

Sorry for the modified names but the logic remains untouched so I think it wont be a problem.

So then I try to add pagination like so :

from  (SELECT tbl.*, ROW_NUMBER() OVER(ORDER BY ID) AS
       rownum FROM YMFK.LK99Z as tbl WHERE ORG.FirstName  like ''%Peter%'' ) ORG
       WHERE ORG.rownum BETWEEN 1 AND 10

which i put on the place of from YMFK.LK99Z ORG and also removing where ORG.FirsName like ''%Peter%'''. This code is something i tried to get form a working pagination query modfying it as little as possible, but the first(original) query is working, and the second(modified) query is giving me error

Column qualifier or table ORG undefined.".

Upvotes: 0

Views: 134

Answers (1)

Paolo
Paolo

Reputation: 2254

in your code:

from  (SELECT tbl.*, ROW_NUMBER() OVER(ORDER BY ID) AS
       rownum FROM YMFK.LK99Z as tbl WHERE ORG.FirstName  like ''%Peter%'' ) ORG
       WHERE ORG.rownum BETWEEN 1 AND 10

the alias ORG is defined outside the inner scope.
try the following:

from  (SELECT tbl.*, ROW_NUMBER() OVER(ORDER BY ID) AS
       rownum FROM YMFK.LK99Z as tbl WHERE tbl.FirstName  like ''%Peter%'' ) ORG
       WHERE ORG.rownum BETWEEN 1 AND 10

Upvotes: 1

Related Questions