Thread: Using functions to calc field values in other table

Using functions to calc field values in other table

From
Robert Fitzpatrick
Date:
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@webtent.org>