在PL / pgSQL中的存储过程中声明临时表的变量

时间:2016-06-02 12:36:13

标签: postgresql plpgsql

我首先收到此错误:

ERROR:  syntax error at or near "conference"
LINE 19: FOR conference IN conferenceset 

这是功能:

CREATE OR REPLACE FUNCTION due_payments_to_suppliers_previous_month() 
RETURNS TABLE(supplier varchar,due_amount numeric) 
AS $$
DECLARE 

BEGIN

 CREATE TABLE conferenceset AS -- temporary table, so I can store the result set
    SELECT 
    conference.conference_supplier_id,
    conference.id AS conferenceid,
    conference.price_per_person, 
    0 AS participants_count,
    400 AS deduction_per_participant,
    0 AS total_amount 
    FROM Conference WHERE --- date_start has to be from the month before
            date_start >= date_trunc('month', current_date - interval '1' month)
            AND 
            date_start < date_trunc('month', current_date);

FOR conference IN conferenceset
LOOP
---fill up the count_participants column for the conference
conference.participants_count := 
    SELECT COUNT(*)
    FROM participant_conference JOIN conferenceset
    ON participant_conference.conference_id = conferenceset.conferenceid;
---calculate the total amount for that conference
conference.total_amount := somerec.participants_count*(conference.price_per_person-conference.deduction_per_participant);
END LOOP;

----we still don't have the name of the suppliers of these conferences
CREATE TABLE finalresultset AS -- temporary table again  
    SELECT conference_supplier.name, conferenceset.total_amount
    FROM conferenceset JOIN conference_supplier
    ON conferenceset.conference_supplier_id = conference_supplier.id

----we have conference records with their amounts and suppliers' names scattered all over this set
----return the result with the suppliers' names extracted and their total amounts calculated
FOR finalrecord IN (SELECT name,SUM(total_amount) AS amount FROM finalresultset GROUP BY name)
LOOP
supplier:=finalrecord.name;
due_amount:=finalrecord.amount; 
RETURN NEXT; 
END LOOP;  

END; $$
LANGUAGE 'plpgsql';

我不知道如何以及在何处声明我所拥有的两个FOR循环所需的变量:conference类型conferencesetfinalrecord我甚至都不确定。 我想也需要嵌套块。这是我的第一个存储过程,我需要帮助。 谢谢。

1 个答案:

答案 0 :(得分:1)

CREATE OR REPLACE FUNCTION due_payments_to_suppliers_previous_month() 
RETURNS TABLE(supplier varchar,due_amount numeric) 
AS $$
DECLARE 
  conference record;
  finalrecord record;
BEGIN

 CREATE TABLE conferenceset AS -- temporary table, so I can store the result set
    SELECT 
    conference.conference_supplier_id,
    conference.id AS conferenceid,
    conference.price_per_person, 
    0 AS participants_count,
    400 AS deduction_per_participant,
    0 AS total_amount 
    FROM Conference WHERE --- date_start has to be from the month before
            date_start >= date_trunc('month', current_date - interval '1' month)
            AND 
            date_start < date_trunc('month', current_date);

FOR conference IN (select * from conferenceset)
LOOP
---fill up the count_participants column for the conference
conference.participants_count = (
    SELECT COUNT(*)
    FROM participant_conference JOIN conferenceset
    ON participant_conference.conference_id = conferenceset.conferenceid
);
---calculate the total amount for that conference
conference.total_amount = somerec.participants_count*(conference.price_per_person-conference.deduction_per_participant);
END LOOP;

----we still don't have the name of the suppliers of these conferences
CREATE TABLE finalresultset AS -- temporary table again  
    SELECT conference_supplier.name, conferenceset.total_amount
    FROM conferenceset JOIN conference_supplier
    ON conferenceset.conference_supplier_id = conference_supplier.id

----we have conference records with their amounts and suppliers' names scattered all over this set
----return the result with the suppliers' names extracted and their total amounts calculated
FOR finalrecord IN (SELECT name,SUM(total_amount) AS amount FROM finalresultset GROUP BY name)
LOOP
supplier = finalrecord.name;
due_amount = finalrecord.amount; 
RETURN NEXT; 
END LOOP;  

END; $$
LANGUAGE 'plpgsql';