I am trying to create a view like this :
CREATE OR REPLACE FUNCTION calculate(datemin DATE,datemax DATE) RETURNS VOID AS
$BODY$
BEGIN
DROP VIEW IF EXISTS zdroits CASCADE;
CREATE VIEW zdroits AS
SELECT r.*
FROM rights r
WHERE r.date_paid BETWEEN datemin AND datemax;
-- the rest of the function will use the view
END;
$BODY$ LANGUAGE plpgsql;
But PostgreSQL won't let me create the view, saying column datemin does not exist
.
If instead, I replace the WHERE
line with this, it is ok :
WHERE r.date_paid BETWEEN '2011-01-01' AND '2016-12-31'
I am not trying to create a parameterized view. I want to use the parameters passed to the function as "literals".
Trying to understand how this answer could help me, I experimented with things like this :
EXECUTE '
CREATE VIEW zdroits AS
SELECT r.*
FROM rights r
WHERE r.date_paid BETWEEN $1 AND $2;
' USING (datemin,datemax);
but the compiler says "there is no parameter $1".
Is there a way to do this ?