Re: scope quirk in copy in function - Mailing list pgsql-general

From Adrian Klaver
Subject Re: scope quirk in copy in function
Date
Msg-id 551020F2.9070903@aklaver.com
Whole thread Raw
In response to scope quirk in copy in function  (Scott Ribe <scott_ribe@elevated-dev.com>)
Responses Re: scope quirk in copy in function
List pgsql-general
On 03/23/2015 06:59 AM, Scott Ribe wrote:
> create or replace function archive_some_stuff() returns void as $$
> declare cutoff timestamptz;
> begin
>    cutoff := now() - '1 day'::interval;
>    copy (select * from log where end_when < cutoff) to ...
>    ...
>
> Gives me an error that there is column named cutoff. (Other uses of cutoff in queries not inside a copy, iow the
deletefrom commands, work.) 
>
> Is there any alternative to just duplicating the now() expression inside every copy?

http://www.postgresql.org/docs/9.3/interactive/plpgsql-implementation.html#PLPGSQL-VAR-SUBST

"Variable substitution currently works only in SELECT, INSERT, UPDATE,
and DELETE commands, because the main SQL engine allows query parameters
only in these commands. To use a non-constant name or value in other
statement types (generically called utility statements), you must
construct the utility statement as a string and EXECUTE it."

>


--
Adrian Klaver
adrian.klaver@aklaver.com


pgsql-general by date:

Previous
From: Igor Stassiy
Date:
Subject: Difference between light-weight geometry types
Next
From: Tom Lane
Date:
Subject: Re: scope quirk in copy in function