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/