Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
821 views
in Technique[技术] by (71.8m points)

function - Error: query has no destination for result data while using a cursor

I have a function which I have written to automate the execution of a group of functions for my project. I am taking a refcursor where I am storing my required data which I will be passing as an argument to each of my functions being called and based on the argument will get executed. I am giving my code here:

CREATE OR REPLACE FUNCTION ccdb.fn_automation()
RETURNS void AS
$BODY$

DECLARE 
sec_col refcursor;
cnt integer;
sec_code ccdb.update_qtable%ROWTYPE;
new_cnt numeric;

BEGIN

SELECT COUNT(*) INTO cnt FROM ccdb.update_qtable WHERE status_flag IN (-1,1);

OPEN sec_col FOR SELECT DISTINCT section_code FROM ccdb.update_qtable WHERE status_flag IN (-1,1);

FOR i IN 1..cnt
LOOP

        FETCH sec_col INTO sec_code;
        SELECT ccdb.o_dtr_update(sec_code.section_code);
        SELECT ccdb.o_consumer_update_for_update(sec_code.section_code);
        SELECT ccdb.o_bills_update_for_update(sec_code.section_code);
        SELECT ccdb.o_payments_update_for_update_new(sec_code.section_code);
        SELECT ccdb.o_payments_map_update_for_update(sec_code.section_code);

        SELECT COUNT(*) INTO new_cnt FROM ccdb.update_qtable WHERE status_flag IN (-1,1);

        IF new_cnt > cnt 
        THEN 
            CLOSE sec_col;

            OPEN sec_col FOR SELECT DISTINCT section_code FROM ccdb.update_qtable WHERE status_flag IN (-1,1);

            cnt := new_cnt;
        END IF;

END LOOP;

CLOSE sec_col;

END;

$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

Now here the problem that I am facing is, whenever I try to execute this function, i get an error saying,

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 ccdb.fn_automation() line 20 at SQL statement

I don't know where am I supposed to use PERFORM in this function. The the context says the error is in line 20, i.e., the SELECT statement which I am using while opening my cursor. So I don't how can I resolve this issue.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As per instructions in the manual:

Sometimes it is useful to evaluate an expression or SELECT query but discard the result, for example when calling a function that has side-effects but no useful result value. To do this in PL/pgSQL, use the PERFORM statement:

PERFORM query;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...