Subject | Re: [firebird-support] Re: random row select |
---|---|
Author | Yosvany Llerena RodrÃguez |
Post date | 2006-10-25T08:23:31Z |
I find other solution with current_timestamp from rdb$database and the
ceiling UDF.
create procedure random_text
returns (ATEXT varchar(150) character set ISO8859_1)
as
declare amax integer;
declare rnumb integer;
declare asecond integer;
begin
rnumb = 0;
select extract(second from current_timestamp) from rdb$database into asecond;
select max(id_text) from table_text into amax;
rnumb = ceiling((:asecond+:asecond)/:amax);
if (:rnumb = 0) then
begin
rnumb = ceiling((40+:asecond)/:amax);
end
select texto from table_text where id_text = :rnumb into :ATEXT;
suspend;
end
ceiling UDF.
create procedure random_text
returns (ATEXT varchar(150) character set ISO8859_1)
as
declare amax integer;
declare rnumb integer;
declare asecond integer;
begin
rnumb = 0;
select extract(second from current_timestamp) from rdb$database into asecond;
select max(id_text) from table_text into amax;
rnumb = ceiling((:asecond+:asecond)/:amax);
if (:rnumb = 0) then
begin
rnumb = ceiling((40+:asecond)/:amax);
end
select texto from table_text where id_text = :rnumb into :ATEXT;
suspend;
end
On 10/25/06, Adam <s3057043@...> wrote:
> --- In firebird-support@yahoogroups.com, "Yosvany Llerena RodrÃguez"
> <yosvanyllr@...> wrote:
> >
> > Hi all.
> >
> > I'm tryin to create and stored procedure to select a random row from a
> > table, of curse in where clasusle i have an integer field. I means
> > random between 1 and the max of this field.
> >
>
> You will need to find/write a UDF function to generate a random
> number. If your source table is fixed, then the procedure is pretty
> simple.
>
> CREATE PROCEDURE FOO
> RETURNS
> (
> BAR INTEGER
> )
> AS
> DECLARE VARIABLE RND INTEGER;
> BEGIN
> RND = RAND(100);
>
> FOR SELECT BAR
> FROM MYTABLE
> WHERE ID = :RND
> INTO :BAR
> DO
> BEGIN
> SUSPEND;
> END
> END
> ^
>
> SELECT BAR
> FROM FOO;
>
> Note that a lot of random number generators are based on the internal
> clock, and so if you include a call to the SP in a tight loop you may
> get the same row several times.
>
> Adam
>
>
>
>
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> Visit http://www.firebirdsql.org and click the Resources item
> on the main (top) menu. Try Knowledgebase and FAQ links !
>
> Also search the knowledgebases at http://www.ibphoenix.com
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> Yahoo! Groups Links
>
>
>
>
>