Re: Nested-Internal Functions

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Rossana Ocampos <rocampos(at)bry-it(dot)com>
Cc: "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Nested-Internal Functions
Date: 2024-01-16 13:24:49
Message-ID: CAKFQuwZOLH7s+sRtO8s3Ptu=TEKtjNM0OYLaJWEMQCxUKuMXkQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tuesday, January 16, 2024, Rossana Ocampos <rocampos(at)bry-it(dot)com> wrote:

> *Hello ,*
>
> *I have a query about creating nested functions in PostgreSQL.*
>
> *I am currently using PostgreSQL 15 and I am trying to create a nested
> function with the following structure:*
>
> *CREATE OR REPLACE FUNCTION external_function ()*
>
> *RETURNS void AS $$*
>
> *DECLARE*
>
> *external_variable;*
>
>
>
> * -- Define the internal function*
>
> * FUNCTION internal_function ()*
>
> * RETURNS void AS $$*
>
> * DECLARE*
>
> * internal_variable INT;*
>
> * BEGIN*
>
> * -- Internal function code*
>
> * internal_variable:= 10;*
>
> * RAISE NOTICE 'Internal Variable: %', internal_variable;*
>
> * END;*
>
> * $$ LANGUAGE plpgsql;*
>
> *BEGIN*
>
> * -- External function code*
>
> * external_variable:= 5;*
>
> * RAISE NOTICE 'External variable: %', external_variable;*
>
>
>
> * -- Call internal function*
>
> * PERFORM internal_function ();*
>
> *END;*
>
> *$$ LANGUAGE plpgsql;*
>
>
>
> However, I get an error, and I can't compile the function.
>
> *Thank you very much for your help*
>
>
You did nested dollar quoting wrong; and I don’t see the word create where
you try to define the function inside the outer function.

You are probably better off just defining two functions independently
anyway, there is minimal benefit to having on function define another in
PostgreSQL, there are no closures.

David J.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rob Sargent 2024-01-16 14:34:49 Re: postgres sql assistance
Previous Message Raul Giucich 2024-01-16 13:00:03 Re: postgres sql assistance