... - Mailing list pgsql-sql

From Dan Langille
Subject ...
Date
Msg-id 20030928151910.S78282@laptop.unixathome.org
Whole thread Raw
Responses Re:  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
I'm trying to create a function which returns a result set using a dynamic
query.  The problem occurs when it compiles.  I suspect it's my quoting,
but I'm not sure of the cause.

CREATE OR REPLACE FUNCTION LoginCounts(int) RETURNS SETOF
logincounts_record AS '
DECLARE   MaxDays ALIAS for $1;
   r   logincounts_record%rowtype;   i   integer;

BEGIN   FOR i IN 1..MaxDays LOOP       EXECUTE ''       SELECT count(*)         INTO r         FROM users        WHERE
lastloginbetween current_date - interval \''' ||
 
quote_literal(i - 1) || '' days\'                            AND current_date - interval \''' ||
quote_literal(i)     || '' days\''';
       RETURN NEXT r;   END LOOP;   RETURN;
END
'
LANGUAGE plpgsql;


# select * from LoginCounts(2);
WARNING:  Error occurred while executing PL/pgSQL function logincounts
WARNING:  line 9 at execute statement
ERROR:  parser: parse error at or near "days" at character 151

thnks

-- 
Dan Langille - http://www.langille.org/


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Mystery function error
Next
From: Tom Lane
Date:
Subject: Re: