Re: Simple question about running a function.

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: SQL Padawan <sql_padawan(at)protonmail(dot)com>
Cc: "pgsql-novice(at)lists(dot)postgresql(dot)org" <pgsql-novice(at)lists(dot)postgresql(dot)org>
Subject: Re: Simple question about running a function.
Date: 2021-11-26 17:39:51
Message-ID: CAKFQuwbXhSd_2udmXNamPyt6Rdv-jsKG7cQ6weP=TnoxCZyHqA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Fri, Nov 26, 2021 at 9:35 AM SQL Padawan <sql_padawan(at)protonmail(dot)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.

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message David G. Johnston 2021-11-26 17:43:42 Re: Simple question about running a function.
Previous Message SQL Padawan 2021-11-26 17:36:58 Re: Simple question about running a function.