Re: function to operate on same fields, different records?

Will Trillich <will@serensoft.com>

From: will trillich <will@serensoft.com>
To: pgsql-general@postgresql.org
Cc: "Eric G. Miller" <egm2@jps.net>
Date: 2001-03-31T06:17:46Z
Lists: pgsql-general
On Fri, Mar 30, 2001 at 06:34:45PM -0800, Eric G. Miller wrote:
> On Fri, Mar 30, 2001 at 06:40:13PM -0600, will trillich wrote:
> > even using PLPGSQL, is it possible to send VARYING relation
> > tuples to a procedure/function -- so long as the attributes
> > (fields) munged within the function are common to all tables?
> 
> Easiest way would be a five parameter function, then it doesn't need to
> know field names, just positions.  I'm assuming you'll have a default of

fooey. i was hoping...

> zero for each grade field (simplifies NULL handling).  An aggregate
> function might be more elegant (but certainly more work).  Guess this
> school doesn't use the +/- modifiers?

only cosmetically. (a+,a,a- == 4.0)

> -- UNTESTED!  Will accept any integer for a grade count...
> 
> CREATE FUNCTION gpa (int4, int4, int4, int4, int4)
>                 --   "a"    "b"   "c"   "d"   "f"
> RETURNS float8 As '
>     DECLARE
>         numer float8 NOT NULL;
> 	denom int4 NOT NULL;

why NOT NULL ?

>     BEGIN
>         numer := 4.0 * $1 + 3.0 * $2 + 2.0 * $3 + 1.0 * $4;
> 	denom := $1 + $2 + $3 + $4 + $5;
> 	
> 	IF denom < 1 THEN
> 	   RETURN 0.0;
> 	END IF;
> 
> 	RETURN numer / denom;
>     END;
> ' LANGUAGE 'plpgsql';

and here i had my hopes up for a universal "no record to big or
too small" function argument... thanks!

-- 
does a brain cell think?

will@serensoft.com
http://sourceforge.net/projects/newbiedoc -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!