Thread: trigger and plpgsq help needed
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;
Sunday Olutayo
On 09/26/2012 10:42 AM, SUNDAY A. OLUTAYO wrote: > 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. I believe what you're looking for is "IF NOT FOUND THEN" instead of "IF last_id IS NULL THEN" Cheers! - Chris
Why not use IF FOUND?
RobR
From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of SUNDAY A. OLUTAYO
Sent: Wednesday, September 26, 2012 12:43 PM
To: pgsql-general@postgresql.org
Subject: [GENERAL] trigger and plpgsq help needed
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.
On 09/26/2012 09:42 AM, SUNDAY A. OLUTAYO wrote: > 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. Is there a NEW.amount? > > Kindly help out. > > > > > Thanks, > > Sunday Olutayo > > -- Adrian Klaver adrian.klaver@gmail.com
I have tried it 'IF FOUND' but not getting the desired result
Sunday Olutayo
To: pgsql-general@postgresql.org
Sent: Wednesday, September 26, 2012 7:14:46 PM
Subject: Re: [GENERAL] trigger and plpgsq help needed
Why not use IF FOUND?
RobR
From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of SUNDAY A. OLUTAYO
Sent: Wednesday, September 26, 2012 12:43 PM
To: pgsql-general@postgresql.org
Subject: [GENERAL] trigger and plpgsq help needed
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.
I have tried "IF NOT FOUND THEN" but not getting the desired result Thanks, Sunday Olutayo Sadeeb Technologies Ltd 7 Mayegun Street, Ojo Lagos State, Nigeria. Tel: +234 1 7404524 D/L: +234 1 8169922 Cell: +234 8054600338, 234 8096441688 Fax: +234 8054895664 mail: olutayo@sadeeb.com ----- Original Message ----- From: "Chris Ernst" <cernst@zvelo.com> To: pgsql-general@postgresql.org Sent: Wednesday, September 26, 2012 7:10:03 PM Subject: Re: [GENERAL] trigger and plpgsq help needed On 09/26/2012 10:42 AM, SUNDAY A. OLUTAYO wrote: > 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. I believe what you're looking for is "IF NOT FOUND THEN" instead of "IF last_id IS NULL THEN" Cheers! - Chris -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general
"IF last_id IS NULL THEN" eventually work
Sunday Olutayo
Sadeeb Technologies Ltd
To: "Rob Richardson" <RDRichardson@rad-con.com>
Cc: pgsql-general@postgresql.org
Sent: Thursday, September 27, 2012 2:04:46 PM
Subject: Re: [GENERAL] trigger and plpgsq help needed
I have tried it 'IF FOUND' but not getting the desired result
Sunday Olutayo
To: pgsql-general@postgresql.org
Sent: Wednesday, September 26, 2012 7:14:46 PM
Subject: Re: [GENERAL] trigger and plpgsq help needed
Why not use IF FOUND?
RobR
From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of SUNDAY A. OLUTAYO
Sent: Wednesday, September 26, 2012 12:43 PM
To: pgsql-general@postgresql.org
Subject: [GENERAL] trigger and plpgsq help needed
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.