postgres SQL状态:运行函数

时间:2017-04-21 19:23:27

标签: sql database postgresql function

Postgres DB

如果我使用查询 SELECT sms.somefunc(); 运行以下函数,则出现错误 SQL状态:P0001

我在这个函数中调用了另一个函数。

有人可以帮我解决这个问题吗?我不知道为什么会出现这种错误。

提前致谢。

--DROP FUNCTION SMS.somefunc();

CREATE FUNCTION SMS.somefunc() RETURNS void AS $BODY$

DECLARE

    wk_rows INT :=0;
    work_plan_id_val INT :=0;
    DECLARE wp_rows     INT DEFAULT 0;
BEGIN

    DECLARE wk_plan_id CURSOR FOR 

        SELECT COUNT(work_plan_id) FROM sms.work_plan 
            WHERE class_general_id = 8 AND
            subject_general_id = 18;

        BEGIN
            OPEN wk_plan_id;
                FETCH wk_plan_id INTO wk_rows;
            CLOSE wk_plan_id;
        END;

    DECLARE wp CURSOR FOR 

        SELECT work_plan_id FROM sms.work_plan 
            WHERE class_general_id = 8 AND
            subject_general_id = 18;  --442

        BEGIN
            OPEN wp;
            IF wk_rows > 0 THEN
                wp_rows = 0;
                WHILE wp_rows < wk_rows LOOP
                    FETCH wp INTO work_plan_id_val;

                    RAISE NOTICE 'work_plan_id_val %', work_plan_id_val;
                    EXECUTE SMS.P_DELETE_X (work_plan_id_val,'admin');
                    RAISE EXCEPTION 'Something happen %', work_plan_id_val;

                    wp_rows = wp_rows + 1;
                    --DBMS_OUTPUT.PUT_LINE('TEST');
                END LOOP;
            END IF;
            CLOSE wp;
        END;

END;

$BODY$ 
LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION SMS.somefunc()
  OWNER TO postgres;

1 个答案:

答案 0 :(得分:0)

您可能会使用语句PERFORM而不是EXECUTE

EXECUTE SMS.P_DELETE_X (work_plan_id_val,'admin');

应该是

PERFORM SMS.P_DELETE_X (work_plan_id_val,'admin');

EXECUTE语句应该用于动态SQL,而不是用于函数执行。

还有一个小问题 - 您应该使用EXISTS代替count(*) - 以及另一个问题 - 这是ISAM风格缓慢的经典示例。