简体   繁体   中英

Using COPY command with subquery in PL\pgSQL

there is an example of function i'm trying to create:

CREATE OR REPLACE FUNCTION exchange_to_csv()
  RETURNS integer AS $$
    DECLARE
    xseq integer;
    BEGIN
      SELECT max(events_stage.seq) FROM events_stage INTO xseq;
      IF xseq is not NULL THEN
        EXECUTE 'COPY (SELECT * from events_stage WHERE events_stage.seq <= $1) TO ''/tmp/test.csv'' WITH CSV' USING xseq;
        DELETE FROM event_stage WHERE event_stage.seq <= xseq;
      END IF;
      RETURN xseq;
    END;
$$ LANGUAGE plpgsql;

Executing this code i get the following error:

[42P02] ERROR: there is no parameter $1

As i understand $1 isn't exist inside subquery. Is there a way, how i can pass this param there?

$1 is the first parameter in your function. Your function exchange_to_csv() has not parameters so $1 is not recognized.

If you want to pass a parameter you need to change your function to:

exchange_to_csv(x Type)

Type can be Integer , Text etc... Then from function body you can access x by its name x or by $1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM