简体   繁体   English

Postgresql-函数获取行作为参数-如何访问该行的列?

[英]Postgresql - Function get row as parameter - How to acces to a column of this row?

I have a function that must get as paramater a row of a table employee and returnd the age of the employee passed as parameter: 我有一个必须将表雇员行作为参数并返回作为参数传递的雇员年龄的函数:

CREATE OR REPLACE FUNCTION getAge(employe employee) RETURNS int AS $$
begin
  return employe.age;
END; $$
LANGUAGE PLPGSQL;

I am trying to call the function like 我试图像这样调用函数

select * from getAge('(1, Alexander, 34, null)' );

But it doesn't work. 但这是行不通的。 I am expecting that the function returns 34. How should I call the function properly? 我期望函数返回34。如何正确调用函数?

I assume your employee table looks somewhat like this: 我假设您的员工表看起来像这样:

knayak=# \d employee
              Table "public.employee"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 id     | integer |           |          |
 name   | text    |           |          |
 age    | integer |           |          |
 col4   | text    |           |          |

Simply call it as 简称为

select getAge ('(1,Alexander,34,null)');
 getage
--------
     34

Or use an table alias from a query as the input. 或使用查询中的表别名作为输入。

select getage(emp)
from employee as emp
where e.id = 1;

select * from func() is used for a SET/TABLE returning functions. select * from func()用于SET/TABLE返回函数。

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM