Re: SELECTing first x results (a FAQ?)
Posted in 1998
Topics: SQL Development & Query Writing
D. Sanderson wrote:
> I want an SELECT statement that returns the first x results (x being a
> constant, say, 50) matching certain criteria. (I want an exact number, so
> I can't use the 'fraction of' formula in the FAQ.)
>
> I don't want a broad user-specified search to tie up the port getting
> hundreds of
> results.
On server side You can use temporary table with serial field.
CREATE TEMP TABLE the_temp_table(id SERIAL, the_data CHAR(x))
INSERT INTO the_temp_table SELECT "0", needed_value FROM some_table WHEREsome_condition
DELETE FROM the_temp_table WHERE id < (50 + 1)
INSERT INTO CLIENT (-:)) SELECT the_data FROM the_temp_table
Actually You get all records that match Your condition, but You store them in
temporary table and get only first 50.
Instead of storing values in temp table You can store some ids of ther
records.
In ORACLE ther is some counter for being getted record.
For example:
CREATE COUNTER rec_counter_STARTED WITH 1 #not real example, not real
syntax
SELECT rec_counter, some_data FROM some_table WHERE some_condition AND
rec_counter < (50+1)
--
#ifdef LANGUAGE=POLISH | Unix Adm & Dev, Informix Adm & Dev
#include <wyrazy_szacunku.h> | Pure HTML Coder, Win32 API Dev
#else
#include <greetings.h>
#endif