Subject | Re: selecting a random record within the db |
---|---|
Author | Adam |
Post date | 2005-06-08T00:25:18Z |
--- In firebird-support@yahoogroups.com, "rebel_rob_98"
<rebel_rob_98@y...> wrote:
you will need to know the record count of the dataset you are
selecting, and a select count(*) is not very efficient. For smallish
tables it is fine.
I can do this with ASP however it requires a
You will also need to find (or write) a UDF to do this. I know rfunc
(rfunc.sourceforge.com) has one but I have not used it.
The basic stored procedure would be:
1. Run the select count(*) and store it in a variable n
2. Using your UDF function, generate a random number between 0 and n-1
and store it in a variable m
3. Run the SQL:
select first 1 skip :n
from ....
(I do not know whether you can use a parameter inside the skip. If you
can't then there is still the option of storing your query in a varchar
and using execute statement to do it).
to add your own.
Adam
<rebel_rob_98@y...> wrote:
> Hello All,Random, I assume you mean uniform distribution. To achieve this goal,
>
> I would like to select a random record from a table and return the
> results on a web page.
you will need to know the record count of the dataset you are
selecting, and a select count(*) is not very efficient. For smallish
tables it is fine.
I can do this with ASP however it requires a
> couple of trips to the database (which I am hoping to avoid).You will need to use a Stored Procedure to do this.
> I was
> hoping to accomplish this selection within a view or stored procedure.
You will also need to find (or write) a UDF to do this. I know rfunc
(rfunc.sourceforge.com) has one but I have not used it.
The basic stored procedure would be:
1. Run the select count(*) and store it in a variable n
2. Using your UDF function, generate a random number between 0 and n-1
and store it in a variable m
3. Run the SQL:
select first 1 skip :n
from ....
(I do not know whether you can use a parameter inside the skip. If you
can't then there is still the option of storing your query in a varchar
and using execute statement to do it).
> However, I can not find any information regarding the availability ofThere is no built-in random number generator, BUT it is not very hard
> rnd() or random() for firebird.
to add your own.
Adam