postgresql手册plpgsql函数声明部分看不懂
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;
是in_t
别名sometable
别名?我尝试通过以下方式复制它:
create function conca_selected_col_emp( in_t public.emp) returns text as $$
begin
return in_t.name || in_t.department;
end;
$$ LANGUAGE plpgsql;
显然,这行不通...
它应该可以工作:
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)
或替代语法:
postgres=# select foo.fx from foo;
┌──────────────┐
│ fx │
╞══════════════╡
│ hello, world │
└──────────────┘
(1 row)
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;
是in_t
别名sometable
别名?我尝试通过以下方式复制它:
create function conca_selected_col_emp( in_t public.emp) returns text as $$
begin
return in_t.name || in_t.department;
end;
$$ LANGUAGE plpgsql;
显然,这行不通...
它应该可以工作:
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)
或替代语法:
postgres=# select foo.fx from foo;
┌──────────────┐
│ fx │
╞══════════════╡
│ hello, world │
└──────────────┘
(1 row)