jian
jian

Reputation: 4824

postgresql manual plpgsql function declaration section not understand

https://www.postgresql.org/docs/14/plpgsql-declarations.html#PLPGSQL-DECLARATION-PARAMETERS

CREATE FUNCTION concat_selected_fields(in_t sometablename) RETURNS text AS $$
BEGIN
    RETURN in_t.f1 || in_t.f3 || in_t.f5 || in_t.f7;
END;
$$ LANGUAGE plpgsql;

is in_t alias sometable alias name? I try to replicate it by following:

create function conca_selected_col_emp( in_t public.emp) returns text as $$
    begin
    return in_t.name || in_t.department;
    end;
$$ LANGUAGE plpgsql;

Obviously, this will not work...

Upvotes: 0

Views: 44

Answers (1)

Pavel Stehule
Pavel Stehule

Reputation: 45770

It should to work:

create table foo(a varchar, b varchar);

insert into foo values('hello', 'world');

create or replace function fx(arg foo)
returns varchar as $$
begin
   return arg.a || ', ' || arg.b;
end;
$$ language plpgsql;

postgres=# select fx(foo) from foo;
┌──────────────┐
│      fx      │
╞══════════════╡
│ hello, world │
└──────────────┘
(1 row)

or alternative syntax:

postgres=# select foo.fx from foo;
┌──────────────┐
│      fx      │
╞══════════════╡
│ hello, world │
└──────────────┘
(1 row)

Upvotes: 1

Related Questions