trigger and plpgsq help needed

Поиск
Список
Период
Сортировка
От SUNDAY A. OLUTAYO
Тема trigger and plpgsq help needed
Дата
Msg-id f071e40e-f985-4716-8e9a-4a83c4b3eca7@mail
обсуждение исходный текст
Ответы Re: trigger and plpgsq help needed  (Chris Ernst <cernst@zvelo.com>)
Re: trigger and plpgsq help needed  (Rob Richardson <RDRichardson@rad-con.com>)
Re: trigger and plpgsq help needed  (Adrian Klaver <adrian.klaver@gmail.com>)
Список pgsql-general
Dear all,

I have issue with the red portion function below;

This IF last_id IS NULL THEN suppose to test if last_id is NULL, that is the select query did not found it
then execute the rest of the red sql but it always fail to insert the NEW.amount into amount, every other things fine.

Kindly help out.



CREATE OR REPLACE FUNCTION invoice_trigger_func() RETURNS trigger AS
$$

DECLARE
last_id integer;
current_balance_id integer;
oldbalance numeric(10,2);
newbalance numeric(10,2);

BEGIN

    SELECT lastbal_id INTO last_id FROM patient
        WHERE patient_id = NEW.patient_id;
        IF last_id IS NULL THEN
           INSERT INTO balance (invoice_id, patient_id, amount)
            VALUES (NEW.invoice_id, NEW.patient_id, NEW.amount);
           UPDATE patient SET lastbal_id = (SELECT currval('balance_balance_id_seq'))
            WHERE patient_id = NEW.patient_id;

            ELSE
           SELECT amount INTO oldbalance FROM  balance
            WHERE balance_id = last_id;
           INSERT INTO balance (invoice_id, patient_id, amount)
            VALUES (NEW.invoice_id, NEW.patient_id, oldbalance + NEW.amount);
           UPDATE patient SET lastbal_id = (SELECT currval('balance_balance_id_seq'))
            WHERE patient_id = NEW.patient_id;
            END IF;
    RETURN NEW;
    
END;
$$ LANGUAGE plpgsql;


Thanks,

Sunday Olutayo


В списке pgsql-general по дате отправления:

Предыдущее
От: Jeff Janes
Дата:
Сообщение: Re: PostgreSQL data loads - turn off WAL
Следующее
От: Chris Ernst
Дата:
Сообщение: Re: trigger and plpgsq help needed