0

In the below postgres function I am passing 'sample' (integer) as an input parma but when I try to print it's value using raise notice inside the function body I get the following error

ERROR: column "sample" does not exist LINE 1: SELECT sample

CREATE OR REPLACE FUNCTION public.test_function(
        sample integer)
        RETURNS json
        LANGUAGE 'sql'
        COST 100
        VOLATILE PARALLEL UNSAFE
    AS $BODY$
    DO  $$
    BEGIN
      raise notice 'test: %',sample;
    END
    $$;
    select json_agg(1);
    $BODY$;

select "test_function"(10);
0

1 Answer 1

3

It's the anonymous pl/pgsql DO block that does not 'see' the sample parameter. Here is a rewrite in pl/pgsql that does 'see' it.

CREATE OR REPLACE function test_function(sample integer)
RETURNS json LANGUAGE plpgsql COST 100 VOLATILE PARALLEL UNSAFE AS
$BODY$
  BEGIN
    raise notice 'test: %',sample;
    return json_agg(1 + sample);
  END
$BODY$;

pl/pgsql DO blocks are more or less encapsulated and can not return anything either.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.