English 中文(简体)
PostgreSQL: Query没有结果数据目的地
原标题:PostgreSQL: Query has no destination for result data

我正试图通过功能使用dblink,从遥控潜水器中取出数据,但发现错误<代码>,结果数据没有目的地。 我正在使用普采克语这样做。

<>功能>:>

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;

www.un.org/Depts/DGACM/index_spanish.htm

SELECT fun();

ERROR: 查询没有结果数据目的地

最佳回答

储存的程序只是取回上次选举考试的结果。 你们需要实际回报价值:

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

你重新犯了错误,因为邮政公司希望这一功能能够退回某种类型的文本,但你的职能并没有回报。

问题回答

Use a plain SQL function instead of PL/PgSQL, or use SELECT INTO and ordinary RETURN.

你发现的错误的原因是,在<>之间没有return,例如:

    BEGIN
        update mytable set lastcodeused = to_char(cast(lastcodeused as INTEGER)+1,  FM999999999999999999 ) where 
        classid = classIdVar and appid= appIdInt 
        RETURNING concat(pageUniqueCode,lastcodeused) as pageUniqueCode
        into taskcode;
        return taskcode;
    END;

If you have this error using a pgplsql procedure or function, and you are sure that the return is defined correctly, there exists a different non-intuitive cause. I needed some time to realize this so I think it is worth sharing. I had a function like this:

CREATE OR REPLACE FUNCTION "db".fn_x(
    id integer)
    RETURNS TABLE(b_val varchar(100), c_val varchar(100)) 
    LANGUAGE  plpgsql 

AS $BODY$
DECLARE 
var_b_val varchar(100);
var_c_val varchar(100);

BEGIN
    select var_b, var_c
        -- Missing INTO clause was the cause of the error.
        var_b_val, var_c_val
        from "db".table_y where y_id = id;

    return query(select var_b_val, var_c_val);
END;
$BODY$;

仅补充说,缺失的<代码>INTO条款使该职能正确运作。

最后,这一错误还可能引发无声的同义词错误。

我创建了一个 rel=“nofollow noreferer”>PL/pgSQL=,如下所示:

CREATE FUNCTION my_func() RETURNS VOID AS $$
BEGIN
  SELECT 2;
END;
$$ LANGUAGE plpgsql;

然后,打电话my_func()的错误如下:

postgres=# SELECT my_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 my_func() line 3 at SQL statement

因此,我替换了SlectT statement,如下所示:

CREATE FUNCTION my_func() RETURNS VOID AS $$
BEGIN
  -- SELECT 2;
  PERFORM 2;
END;
$$ LANGUAGE plpgsql;

然后,我可以打电话到<条码>my_func(,没有错误,如下所示:

postgres=# SELECT my_func();
 my_func
---------

(1 row)

并且,我设立了一个PL/pgSQL职能,具体如下:

CREATE FUNCTION my_func() RETURNS INTEGER AS $$
BEGIN
SELECT 2;
END;
$$ LANGUAGE plpgsql;

然后,打电话my_func()的错误如下:

postgres=# SELECT my_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 my_func() line 3 at SQL statement

因此,我用替换了<代码>。

CREATE FUNCTION my_func() RETURNS INTEGER AS $$
BEGIN
-- SELECT 2;
RETURN 2; -- Here
END;
$$ LANGUAGE plpgsql;

或,我将<代码>SlectT的说明载于RETURN说明如下:

CREATE FUNCTION my_func() RETURNS INTEGER AS $$
BEGIN
RETURN (SELECT 2); -- Here
END;
$$ LANGUAGE plpgsql;

然后,我可以打电话到<条码>my_func(,没有错误,如下所示:

postgres=# SELECT my_func();
 my_func
---------
       2
(1 row)




相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...