PostgreSQL: Query has no destination for result data

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

I am trying to fetch data from remote db by using dblink through function but getting an error "query has no destination for result data". I am using plpgsql language to do the same.

Function:

CREATE OR REPLACE FUNCTION fun()   RETURNS text AS $$ begin select dblink_connect(       'port=5432 dbname=test user=postgres password=****');  WITH a AS ( SELECT * FROM dblink(     'SELECT slno,fname,mname,lname     FROM    remote_tbl'      ) AS t (slno int, fname text, mname text, lname text) ) , b AS ( INSERT INTO temptab1 SELECT slno, name FROM   a ) , c AS ( INSERT INTO temptab2 SELECT slno, name FROM   a ) INSERT INTO temptab3 SELECT slno, name FROM   a;   select dblink_disconnect(); end; $$  LANGUAGE plpgsql; 

Calling Function:

select fun(); 

Error: query has no destination for result data

回答1:

The stored procedure won't just return the result of the last SELECT. You need to actually return the value:

CREATE OR REPLACE FUNCTION fun() RETURNS text AS $$ DECLARE     result text; BEGIN     --- ....     SELECT dblink_disconnect() INTO result;     RETURN result; END $$ LANGUAGE plpgsql; 

You're getting the error because Postgres expects the function to return something of type text, but your function doesn't return anything.



回答2:

Answer from SL2 was right, but you can write it in the short way:

CREATE OR REPLACE FUNCTION fun() RETURNS text AS $$ BEGIN     --- ....     RETURN(SELECT dblink_disconnect()); END $$ LANGUAGE plpgsql; 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!