Subject | Re: [firebird-support] First word after in alphabetical order |
---|---|
Author | Eric SIBERT |
Post date | 2005-08-28T09:54:16Z |
> As I understand it, you want N + 1 + N rows: the first row where the NAMESee the other message for database definition.
> starts with 'ASTRING' and the N rows above and below that row in the
> ordered set. I assume ID_NAME is the primary key (or some other unique
> constraint) of type BigInt and that NAME is stored as a varchar(40).
>I'mYes
> also assuming that you want a neighbourhood set for the "nearest", even if
> the target name doesn't exist,
> Here's how the SELECT procedure would be:What I really did is :
COMMIT WORK;
SET AUTODDL OFF;
SET TERM ^ ;
/* Stored procedures */
CREATE PROCEDURE "GetMySet"
(
"INPUTSTR" CHAR(8),
"N" SMALLINT
)
RETURNS
(
"ID_WP" INTEGER
)
AS
declare variable TARGET_ID Integer = 0;
declare variable COUNTER Integer = 0;
declare variable OUTPUT_BEGUN INteger = 0;
begin
/* get the PK value of the first occurrence of neighbourhood member
or nearest preceding */
for select ID_WAYPOINT from WAYPOINTS
where WP_NOM <= :INPUTSTR
order by WP_NOM desc
into :TARGET_ID do
begin
if (counter = N + 1) then
break;
counter = counter + 1;
end
counter = 0;
for select ID_WAYPOINT from WAYPOINTS
order by WP_NOM asc
into :ID_WP do
begin
if (counter = 0 and ID_WP = TARGET_ID) then
OUTPUT_BEGUN = 1;
if (OUTPUT_BEGUN = 1) then
begin
counter = counter + 1;
if (counter <= 1 + (N * 2)) then
suspend; -- output a record
else
Break;
end
end
end
^
SET TERM ; ^
COMMIT WORK;
SET AUTODDL ON;
It is slightly better with query at the beginning of alphabet (40s) but
worth at the end (4 mn).
Eric