我正在尝试编写一个返回ROWTYPE的函数,但它应该只返回一条记录。
这是我的功能:
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;
我也试过
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;
其中没有编译。
答案 0 :(得分:1)
要返回查询返回的第一条记录,请执行以下操作:
...
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;
你正在尝试使用一个返回多条记录的查询select...into...
(尽管有order by
)。这在PL / SQL中不起作用。那,你正在尝试Oracle方言中不存在的各种SQL关键字。