Re: [GENERAL] Basic SQL Function question + existance of SPL?

From: Jose Soares <jose(at)sferacarta(dot)com>
To: mathprof(at)bigfoot(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: [GENERAL] Basic SQL Function question + existance of SPL?
Date: 2000-02-02 13:35:01
Message-ID: 38983284.8D59853A@sferacarta.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)bigfoot(dot)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(at)sferacarta(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Nicolas Huillard 2000-02-02 14:39:31 RE: [GENERAL] Rebuilding 6.5.3 RPMs
Previous Message Arnaud FLORENT 2000-02-02 13:04:12 Re: [GENERAL] VACUUM ANALYSE