Mega Code Archive

 
Categories / Delphi / Examples
 

How to return N (or top N) rows using InterBase

Title: How to return "N" (or top N) rows using InterBase Question: How does one limit the number of rows included in a query: e.g. how does one get the top (or bottom) 10 sales totals? Answer: Use a stored procedure that accepts the number of rows you want returned (this is a little more flexible than hard coding the number of rows). create procedure p_get_n_records(iMaxRecords INTEGER) returns () as DECLARE VARIABLE iCount INTEGER; BEGIN iCount = 0; FOR SELECT FROM INTO : DO BEGIN iCount = iCount + 1; IF (iCount SUSPEND; ELSE EXIT; END END