简体   繁体   English

PL/SQL 从函数返回 ROWTYPE

[英]PL/SQL Return ROWTYPE from Function

I am trying to write a function that returns a ROWTYPE, but it should only return one record.我正在尝试编写一个返回 ROWTYPE 的函数,但它应该只返回一条记录。

Here is my function:这是我的功能:

FUNCTION f_get_lastest_by_id (p_id IN T_PERSON_ATTRIBUTE.ID%TYPE)
      RETURN T_PERSON_ATTRIBUTE%ROWTYPE
   IS
      out_rec   T_PERSON_ATTRIBUTE%ROWTYPE;
   BEGIN
        SELECT *
          INTO out_rec
          FROM T_PERSON_ATTRIBUTE
         WHERE ID = p_id
      ORDER BY EFFECTIVE_DATE DESC
      OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;

      RETURN out_rec;
   END;

I've also tried我也试过

FUNCTION f_get_lastest_by_id (p_id IN T_PERSON_ATTRIBUTE.ID%TYPE)
      RETURN T_PERSON_ATTRIBUTE%ROWTYPE
   IS
      out_rec   T_PERSON_ATTRIBUTE%ROWTYPE;
   BEGIN
        SELECT *
          INTO out_rec
          FROM T_PERSON_ATTRIBUTE
         WHERE ID = p_id
      ORDER BY EFFECTIVE_DATE DESC
      LIMIT 1;

      RETURN out_rec;
   END;

Non of which will compile.非其中将编译。

To just return the first record returned by your query do:要仅返回查询返回的第一条记录,请执行以下操作:

...
begin
  for rec in (select *
                from t_person_attribute
               where id = p_id
            order by effective_date desc) loop
    return rec; -- just return the first one we find
  end loop;
end;

You were trying a select...into... with a query that returns more than one record ( order by notwithstanding).您正在尝试使用select...into...查询返回多个记录(尽管order by )。 That will not work in PL/SQL.这在 PL/SQL 中不起作用。 That, and you were trying various SQL keywords that don't exist in the Oracle dialect.那,并且您正在尝试 Oracle 方言中不存在的各种 SQL 关键字。

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

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