Re: Variable LIMIT and OFFSET in SELECTs

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Reg Me Please" <regmeplease(at)gmail(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Variable LIMIT and OFFSET in SELECTs
Date: 2007-11-15 08:38:13
Message-ID: 871warykcq.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Reg Me Please" <regmeplease(at)gmail(dot)com> writes:

> Of course, in my opinion at least, there's no real reason for the above
> syntax limitation, as the sematics is not.

Is not what? Is not sensible?

> create or replace function f_limoff_1( l int, o int )
> returns setof atable as $$
> select * from atable limit $1 offset $2
> $$ language sql;

I would guess what you're looking for is something like this:

select * from atable
where ...
order by ...
limit (select l from limoff where ...)
offset (select o from limoff where ...)

I can't think of any way to get those two subqueries down to one though.

If you create a set returning function like you have above then you can do
funny things with it to, eg, return all the rows in the ranges concatenated.

select (f_limoff_1(l,o)).*
from limoff
where ...

Note that you'll want to modify your function to include an ORDER BY

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Get trained by Bruce Momjian - ask me about EnterpriseDB's PostgreSQL training!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ottavio Campana 2007-11-15 09:12:27 Re: stripping HTML, SQL injections ...
Previous Message சிவகுமார் மா 2007-11-15 07:54:04 Enforcing Join condition