简体   繁体   English

plpgSQL从功能返回记录类型

[英]plpgSQL return record type from FUNCTION

I'm trying to use a plgSQL witch return me a record type : 我正在尝试使用plgSQL女巫返回记录类型:

CREATE FUNCTION actu(id INTEGER) RETURNS RECORD  AS $$
    DECLARE 
      ret RECORD;
    BEGIN
      SELECT id_photo, id_user, lien, titre 
            FROM photo 
            WHERE id_user IN (
                                SELECT id_userabo 
                                FROM abo 
                                WHERE id_user = id ) 
            ORDER BY date_publi DESC LIMIT 10;

    RETURN ret;
    END;$$
LANGUAGE plpgsql;

When I'm trying to use it with : 当我尝试将其与结合使用时:

SELECT * FROM actu(4)
AS (id_photo Integer, id_photo Integer, lien Varchar, titre Varchar);

pgAdmin4 send me error : pgAdmin4给我发错误:

ERROR: ERROR: the request has no destination for the resulting data HINT: If you want to cancel the results of a SELECT, use PERFORM instead. 错误:错误:该请求没有结果数据的目的地。提示:如果要取消SELECT的结果,请改用PERFORM。 CONTEXT: PL/pgsql function fil_actu(integer), line 5 with SQL statement 上下文:PL / pgsql函数fil_actu(integer),带有SQL语句的第5行

You may define those types inside the function, but with different names for the types. 您可以在函数内部定义这些类型,但是这些类型使用不同的名称。 The return type can be a TABLE type and use RETURN QUERY to return the results. 返回类型可以是TABLE类型,并使用RETURN QUERY返回结果。

CREATE FUNCTION actu(id INTEGER) RETURNS TABLE 
(typ_id_photo Integer, typ_id_user Integer, typ_lien Varchar, typ_titre Varchar) 
AS $$
    BEGIN
  RETURN QUERY   
     SELECT id_photo, id_user, lien, titre 
  FROM photo p 
  WHERE id_user IN (SELECT id_userabo 
                    FROM abo 
                    WHERE id_user = id ) 
  ORDER BY date_publi DESC 
  LIMIT 10;

    END;$$
LANGUAGE plpgsql;

The immediate error is, that the result of a select statement needs to be stored somewhere (that's what the error says). 直接的错误是,一条select语句的结果需要存储在某个地方(这就是错误的意思)。 You would need to use select .. into ret from ... to store the result, but that wouldn't work as a variable of type record can only store one row from a result. 您将需要使用select .. into ret from ...来存储结果,但这将不起作用,因为类型为record的变量只能存储结果中的一行。

You apparently want to return more than just one row, so you need to define the function as returns table() and then use return query in PL/pgSQL to return the result of a query. 您显然要返回的不仅仅是一行,因此您需要将函数定义为returns table() ,然后在PL / pgSQL中使用return query结果。 But for a simple function encapsulating a SELECT query, a language sql function is more efficient. 但是对于封装SELECT查询的简单函数, language sql函数效率更高。

CREATE FUNCTION actu(id INTEGER) 
  -- adjust the data types for the returned columns!
  RETURNS table (id_photo int, id_user int, lien text, titre text)
AS $$
  SELECT id_photo, id_user, lien, titre 
  FROM photo 
  WHERE id_user IN (SELECT id_userabo 
                    FROM abo 
                    WHERE id_user = id ) 
  ORDER BY date_publi DESC 
  LIMIT 10;
$$
LANGUAGE sql;

You can use that function like this: 您可以像这样使用该功能:

select *
from actu(42);

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

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