任何人都可以帮我解决这个错误吗?

时间:2014-02-20 11:09:09

标签: function postgresql loops compiler-errors

CREATE OR REPLACE FUNCTION ccdb_dummy.o_bills_new(a integer)
  RETURNS void AS
$BODY$
DECLARE
bill_no bigint;
lc_cin_num bigint;
lc_consumer_num bigint;
lc_bill_type_group_code character varying(10);
t  ccdb_stg.o_bills_stg%ROWTYPE;
t1 ccdb_stg.o_bills_details_stg%ROWTYPE;
BEGIN

    FOR t IN
    SELECT * 
    FROM ccdb_stg.o_bills_stg
    WHERE section_code = a
LOOP
    bill_no := nextval('ccdb_stg_test.bills_seq');

    IF t.consumer_num IS NULL THEN
        lc_consumer_num = t.applicant_num;
    ELSE
        lc_consumer_num = t.consumer_num;

    SELECT bill_type 
    INTO lc_bill_type_group_code
    FROM ccdb_dummy.bill_type_master a
    WHERE a.bill_type_id = t.bill_type_id;

    SELECT cin
    INTO lc_cin_num
    FROM ccdb_dummy.consumers b
    WHERE b.consumer_num = lc_consumer_num;

    INSERT INTO  ccdb_dummy.bills(bill_id,source_system_id,mbc_bill_id,mbc_bill_no,cin,cust_connection_id,consumer_number,cust_type_flag,bill_type_group_code,bill_type_code,bill_month,total_consumption,bill_date,due_date,dc_date,dues_remind_date,dc_remind_date,org_unit_id,parent_bill_id,category_flag,status_flag,conn_cat_subgroup_id,dispute_flag,inst_flag,approved_date,bill_amt,paid_amt,past_arrear,arrear_collected,advance_amount,creation_dt,created_by)
VALUES (bill_no,1,t.mbc_bill_id,t.mbc_bill_no,lc_cin_num,t.cust_connection_id,lc_consumer_num,t.cust_type_flag,t.bill_type_group_code,lc_bill_type_group_code,t.bill_month,
    t.total_consumption,t.bill_date,t.due_date,t.dc_date,t.dues_remaind_date,t.dc_remaind_date,t.section_code,t.parent_bill_id,t.category_flag,t.status_flag,
    t.conn_cat_subgroup_id,t.dispute_flag,t.inst_flag,t.approved_date,t.bill_amt,t.paid_amt,t.past_arrear,t.arrear_collected,t.advance_amount,now(),'system');

    FOR t1 IN
        SELECT *
        FROM ccdb_stg.o_bills_details_stg a 
        WHERE a.mbc_bill_id = t.mbc_bill_id
    LOOP
        INSERT INTO ccdb_dummy.bill_head_details(bill_id,charge_head_code,amount_billed,amount_paid,charge_head_descr,ccdb_update_time,creation_dt,created_by,tariff_id,
                             demand_date)
        VALUES
            (bill_no,t1.charge_head_code,t1.amount_billed,t1.amount_paid,null,now(),now(),'system',t1.tariff_id,t1.demand_date);
    END LOOP;
END LOOP;

END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION ccdb_dummy.o_bills_new(integer)
  OWNER TO ccdb_admin;

当我尝试编译此函数时,出现以下错误:

ERROR:  syntax error at or near "LOOP"
LINE 58:  END LOOP;
              ^
********** Error **********
ERROR: syntax error at or near "LOOP"
SQL state: 42601
Character: 2282

有人可以帮我解决我的错误吗?我确信它一定是傻事,但我无法弄清楚。请有人最早帮助我。

1 个答案:

答案 0 :(得分:1)

我认为你错过了END IF。