SQL查询的函数没有结果数据的目标

时间:2023-01-26 22:57:34

I am trying to create a function that returns a SELECTed resultset. When I call my postgres function like this select * from tst_dates_func() I get an error as shown below:

我正在尝试创建一个返回SELECTed结果集的函数。当我这样调用我的postgres函数select * from tst_dates_func()时,我得到一个错误,如下所示:

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function "tst_dates_func" line 3 at SQL statement

********** Error **********

ERROR: query has no destination for result data
SQL state: 42601
Hint: If you want to discard the results of a SELECT, use PERFORM instead.
Context: PL/pgSQL function "tst_dates_func" line 3 at SQL statement

Here is the function I created:

这是我创建的函数:

CREATE OR REPLACE FUNCTION tst_dates_func() 
    RETURNS TABLE( date_value date, date_id int, date_desc varchar) as
$BODY$   
BEGIN
    select a.date_value, a.date_id, a.date_desc from dates_tbl a;
END;
$BODY$
      LANGUAGE plpgsql;

I am not sure why I am getting the above error. I would like to run select * from tst_dates_func(); and get data back. Or further join the result set if needed. What is the problem here?

我不知道为什么我会收到上述错误。我想从tst_dates_func()运行select *;并获取数据。或者根据需要进一步加入结果集。这里有什么问题?

1 个解决方案

#1


32  

Do it as plain SQL

这样做是纯SQL

CREATE OR REPLACE FUNCTION tst_dates_func() 
    RETURNS TABLE( date_value date, date_id int, date_desc varchar) as
$BODY$   
    select a.date_value, a.date_id, a.date_desc from dates_tbl a;

$BODY$
      LANGUAGE sql;

If you really need plpgsql use return query

如果你真的需要plpgsql使用return query

CREATE OR REPLACE FUNCTION tst_dates_func() 
    RETURNS TABLE( date_value date, date_id int, date_desc varchar) as
$BODY$   
BEGIN
    perform SELECT dblink_connect('remote_db');
    return query
    select a.date_value, a.date_id, a.date_desc from dates_tbl a;

END;
$BODY$
      LANGUAGE plpgsql;

#1


32  

Do it as plain SQL

这样做是纯SQL

CREATE OR REPLACE FUNCTION tst_dates_func() 
    RETURNS TABLE( date_value date, date_id int, date_desc varchar) as
$BODY$   
    select a.date_value, a.date_id, a.date_desc from dates_tbl a;

$BODY$
      LANGUAGE sql;

If you really need plpgsql use return query

如果你真的需要plpgsql使用return query

CREATE OR REPLACE FUNCTION tst_dates_func() 
    RETURNS TABLE( date_value date, date_id int, date_desc varchar) as
$BODY$   
BEGIN
    perform SELECT dblink_connect('remote_db');
    return query
    select a.date_value, a.date_id, a.date_desc from dates_tbl a;

END;
$BODY$
      LANGUAGE plpgsql;