Re: Simple question about running a function. - Mailing list pgsql-novice

From David G. Johnston
Subject Re: Simple question about running a function.
Date
Msg-id CAKFQuwbXhSd_2udmXNamPyt6Rdv-jsKG7cQ6weP=TnoxCZyHqA@mail.gmail.com
Whole thread Raw
In response to Simple question about running a function.  (SQL Padawan <sql_padawan@protonmail.com>)
List pgsql-novice
On Fri, Nov 26, 2021 at 9:35 AM SQL Padawan <sql_padawan@protonmail.com> wrote:
create or replace function test_fn()
returns VOID as $$
DECLARE
BEGIN
  FOR r IN 1..10000 LOOP

    SELECT ('a string');

  END LOOP;
END;
$$ LANGUAGE plpgsql;

Compiles no problems - CREATE FUNCTION is returned as expected.

By default there isn't much compiling going on here.  The pl/pgsql code is just a string that gets executed during query execution.


So, I try:

SELECT test_fn();

but receive the error:

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function test_fn() line 6 at SQL statement

It's complaining that "SELECT ('a string')" doesn't have a destination.  The fact that you got it to work when you removed that select and replaced it with an insert proves that.  The CONTEXT line also tells you this in no uncertain terms.

David J.

pgsql-novice by date:

Previous
From: SQL Padawan
Date:
Subject: Re: Simple question about running a function.
Next
From: "David G. Johnston"
Date:
Subject: Re: Simple question about running a function.