LIMIT ALL == LIMIT NULL

This maybe trivial but I think it is worth sharing anyway
Consider using LIMIT/OFFSET in a function where as parameter you have to specify values for both.
In the following function both limit and offset parameters are integers. This will cause an error when you want to pass ALL (for LIMIT ALL) as argument.

PostgreSQL function:

create or replace function sp_category_getlist
	(plimit integer,poffset integer) returns setof category as
$$
	select *
	from
		category
        order by
		caption
	limit
		$1 offset $2
$$
language sql;

Fortunately PostgreSQL accepts NULL function arguments which in this case is treated as ALL

Example in PHP:

/* sp_filesystem_category_getlist */
public function sp_filesystem_category_getlist
	($puserid,$plimit=null,$poffset='0')
{
	$args = array($plimit,$poffset);
	$sql = "select * from sp_filesystem_category_getlist($1,$2)";
	$result=$this->query($sql,$args);
	return pg_fetch_all($result);
}

This entry was posted on Thursday, February 2nd, 2006 at 10:39 am and is filed under PostgreSQL. You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.

Comments are closed.