Declare variable for SELECT statement in Postgres function I have modified my function, but I have problems with declaring variables. I use Postgres 8.4.

CREATE OR REPLACE FUNCTION requestcounterid(_mindate timestamptz, _maxdate timestamptz) 
  RETURNS TABLE (kategorien text, requestcounter int) AS
$func$  
DECLARE
_minid bigint;
_maxid bigint;

BEGIN 

SELECT id  INTO _minid from tablename
where starttime &gt;= $1 ORDER BY starttime ASC LIMIT 1; 
SELECT id  INTO _maxid from tablename
where starttime &lt; $2 ORDER BY starttime DESC LIMIT 1; 

SELECT CASE WHEN duration &lt;= 10000000 THEN '00-01 sec'::text
            WHEN duration &lt;= 40000000 THEN '01-04 sec'
            WHEN duration &lt;= 100000000 THEN '04-10 sec' 
            WHEN duration &lt;= 300000000 THEN '10-30 sec' 
            WHEN duration &lt;= 600000000 THEN '30-60 sec' 
            ELSE 'more than 60 sec' END  
     , count(*)::int                     
FROM   tablename
WHERE  id &gt;= _minid and id &lt;= _maxid
GROUP  BY 1                              
ORDER  BY 1; 

END; 
$func$ LANGUAGE plpgsql;


Error: 


ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function "requestcounterid" line 12 at SQL statement



How to fix this?
If the SELECT with the CASE expression is something you want to return in the output table, just add RETURN QUERY before it:

RETURN QUERY SELECT CASE ...


Note: 8.4 is very old now.  Even 9.0 is out of support by now - consider upgrading to a recent major version soon.  The old ones usually don't get any security (and other) fixes anymore.