Subject Re: [IBDI] Firebird 1 - Selecting a range of records from a result set.
Author hannes hernler
----- Original Message -----
From: "Peter Morris" <pmorris@...>
> Hi Hannes
> I am not quite following this example (derr).
> Would you mind showing me an exact example ?
> 1) How you select the first page
> 2) How you select page 2

my real-life-example from www.matchpoint.at is:
---------------------------------------------
create PROCEDURE SP_QUICKMATCH (pWORD VARCHAR(80),
pCLASS INTEGER, pLOWBOUND INTEGER, pRANGE INTEGER)
RETURNS (rPID INTEGER, rMATCHCNT INTEGER, rSORTID INTEGER)
AS
begin
SELECT count(distinct q.PROFILE_ID)
FROM QFX q
where (q.QWORD containing :pWord or q.QKEYWORDS containing :pWord )
and (qclass=:pClass or qlevel=:pClass)
into :rMatchCnt;
if (rMatchCnt<pLOWBOUND) then
pLOWBOUND = pLOWBOUND - pRANGE ;
if (pLOWBOUND<1) then
pLOWBOUND = 1;

rSortID = 1;

for
SELECT distinct p.ppid
FROM QFX q JOIN PROFILES p
on q.PROFILE_ID=p.PPID
and (q.QWORD containing :pWord or q.QKEYWORDS containing :pWord )
and (qclass=:pClass or qlevel=:pClass)
order by p.name, p.name2
into :rPID
do
begin
if (rSortID>=pLOWBOUND) then
suspend;
if (rSortID>=(pLOWBOUND+PRANGE-1)) then
exit;
rSortID = rSortID +1;
end
end
^
---------------------------------------------
the part with
" SELECT .. and (q.QWORD containing :pWord ...."
has to be optimized depending on your indices and join strucutre.

I use this in a website to get profiles of people with matching
qualifications in table QFX (www.matchpoint.at)
pLowBound is the number of the first item to show, pRange ist
the number of items per page.

the link in the webpage to previous page:
<A href="matchlist.dws?TID=<%=TID%>&ffPCLASS=<%=Formvar('ffPCLASS')%>
&ffKeyWords=<%= Formvar('ffKeyWords')%>
&fflowrange=<%=iLowBound-iMatchesPerPage%>">prev page</A>

thats it. easy. but as one has said former in this thread: only for this
special relation. if you want to do that with different relations you have
to build
a stored procedure for each.
(the <% %> are tags of Delphi WebScript, embedded server side scripting,
look at http://www.dwscript.com)

hannes hernler
--------------------------------------------------------
DWS - Delphi Web Script
developing professional web apps
look at http://www.dwscript.com
--------------------------------------------------------