Using functions to calc field values in other table

From: Robert Fitzpatrick <robert(at)webtent(dot)org>
To: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Using functions to calc field values in other table
Date: 2013-06-18 15:54:19
Message-ID: 51C082AB.4050102@webtent.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I was wondering if it is possible to store a function in a table for
calculating a value for a field in another table. I am creating a sales
quote tool where users can pick a service to add to the quote_services
table when building the quote. I have a services table where some
services have a static price and others need to be a calculation. The
services table has a numeric price field to use when static pricing is
needed. Would it be possible to add a couple of other fields, say
service_options where I could enter the value 'calc' that would tell my
before TRIGGER on the quote_services table to use a function also stored
in the services table in another field called service_func? When someone
selects the service for the quote, it would execute the function to
calculate and enter the resulting price from the function.

My TRIGGER on the quote_services table would look something like this...

SELECT INTO aRec service_options, service_func FROM services WHERE
service_id = NEW.quote_service;
IF aRec.service_options = 'calc' THEN
NEW.service_price := aRec.service_func || '(' || NEW.service_id || ')';
END IF;
RETURN NEW;

I realize the syntax above may not work, just trying to get across my
idea and hope for some guidance how all this could be done, if possible.

Thanks for any pointers!

--
Robert

--
Robert <robert(at)webtent(dot)org>

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Janes 2013-06-18 17:02:12 Re: I want to make an example of using parameterized path
Previous Message Andreas Kretschmer 2013-06-18 13:30:21 Re: Type cast errors in version 9.2 while upgrade