Re: [GENERAL] Basic SQL Function question + existance of SPL? - Mailing list pgsql-general

From Jose Soares
Subject Re: [GENERAL] Basic SQL Function question + existance of SPL?
Date
Msg-id 38983284.8D59853A@sferacarta.com
Whole thread Raw
In response to Basic SQL Function question + existance of SPL?  (mathprof@bigfoot.com)
List pgsql-general
Try PL/pgSQL language.


CREATE FUNCTION foo(float8) RETURNS float8 AS '
declare
        f1 float8;
begin
        f1 = $1 + 1;
        return f1;
end;' language 'plpgsql';


select foo(2.3);
foo
---
3.3
(1 row)


mathprof@bigfoot.com wrote:

> Apologies if this question is too simple, but I couldn't find an example
> in the manual.
>
> If I'm writing a function in SQL, how do I use the value of a passed
> argument? For example, consider:
>
> CREATE FUNCTION foo(float8) RETURNS float8 AS
> 'select 1.0 as result' language 'sql';
>
> select foo(2.3) as answer;
>
> This works great, but I'd like to write a function where the result
> actually depends on the argument-- as a simple example, I'd like to get
> back the argument plus 1 (eg, "select foo(2.3) as answer" yields 3.3 where
> as "select foo(4.3) as answer" yields 5.3, and so on. How do I do this?
>
> Also, does PostgreSQL support SPL or any sort of procedural language? I'd
> like to write the following (highly-recursive) function:
>
> f(x) = (x*x/120-1/6)*x*x+1)*x for 0<=x<1.57
> f(x) = f(3.14-x) for 1.57<=x<=3.14
> f(x) = -f(x-3.14) for 3.14<=x<6.28
> f(x) = f(x-6.28) for x>=6.28
> f(x) = f(x+6.28) for x<0
>
> (this is roughly the sin() function).
>
> In SPL, I could write this something like:
>
> IF (x<1.57) RETURN (SELECT (x*x/120-1/6)*x*x+1)*x as ANSWER);
> IF (x>=1.57 AND x<=3.14) RETURN (SELECT f(3.14-x) as ANSWER);
> ...
>
> and so on. Does PostgreSQL support anything like that? If not, is there an
> SQL query which will give me the function I want?
>
> ************

> --

Jose' Soares
Bologna, Italy                     Jose@sferacarta.com



pgsql-general by date:

Previous
From: Arnaud FLORENT
Date:
Subject: Re: [GENERAL] VACUUM ANALYSE
Next
From: Nicolas Huillard
Date:
Subject: RE: [GENERAL] Rebuilding 6.5.3 RPMs