Commit c99e851e authored by Bruce Momjian's avatar Bruce Momjian

Clean up sql functions examples.

parent a9876533
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/xfunc.sgml,v 1.15 2000/05/18 14:24:32 momjian Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/xfunc.sgml,v 1.16 2000/05/20 11:24:37 momjian Exp $
--> -->
<chapter id="xfunc"> <chapter id="xfunc">
...@@ -87,11 +87,13 @@ $Header: /cvsroot/pgsql/doc/src/sgml/xfunc.sgml,v 1.15 2000/05/18 14:24:32 momji ...@@ -87,11 +87,13 @@ $Header: /cvsroot/pgsql/doc/src/sgml/xfunc.sgml,v 1.15 2000/05/18 14:24:32 momji
which might be used to debit a bank account: which might be used to debit a bank account:
<programlisting> <programlisting>
create function TP1 (int4, float8) returns int4 CREATE FUNCTION tp1 (int4, float8)
as 'update BANK set balance = BANK.balance - $2 RETURNS int4
where BANK.acctountno = $1 AS ' UPDATE bank
select(x = 1)' SET balance = bank.balance - $2
language 'sql'; WHERE bank.acctountno = $1;
SELECT 1;'
LANGUAGE 'sql';
</programlisting> </programlisting>
A user could execute this function to debit account 17 by $100.00 as A user could execute this function to debit account 17 by $100.00 as
...@@ -108,7 +110,7 @@ select (x = TP1( 17,100.0)); ...@@ -108,7 +110,7 @@ select (x = TP1( 17,100.0));
<programlisting> <programlisting>
select function hobbies (EMP) returns set of HOBBIES select function hobbies (EMP) returns set of HOBBIES
as 'select (HOBBIES.all) from HOBBIES as 'select HOBBIES.* from HOBBIES
where $1.name = HOBBIES.person' where $1.name = HOBBIES.person'
language 'sql'; language 'sql';
</programlisting> </programlisting>
...@@ -123,8 +125,10 @@ select function hobbies (EMP) returns set of HOBBIES ...@@ -123,8 +125,10 @@ select function hobbies (EMP) returns set of HOBBIES
simply returns a base type, such as <literal>int4</literal>: simply returns a base type, such as <literal>int4</literal>:
<programlisting> <programlisting>
CREATE FUNCTION one() RETURNS int4 CREATE FUNCTION one()
AS 'SELECT 1 as RESULT' LANGUAGE 'sql'; RETURNS int4
AS 'SELECT 1 as RESULT;'
LANGUAGE 'sql';
SELECT one() AS answer; SELECT one() AS answer;
...@@ -149,8 +153,10 @@ SELECT one() AS answer; ...@@ -149,8 +153,10 @@ SELECT one() AS answer;
and $2: and $2:
<programlisting> <programlisting>
CREATE FUNCTION add_em(int4, int4) RETURNS int4 CREATE FUNCTION add_em(int4, int4)
AS 'SELECT $1 + $2;' LANGUAGE 'sql'; RETURNS int4
AS 'SELECT $1 + $2;'
LANGUAGE 'sql';
SELECT add_em(1, 2) AS answer; SELECT add_em(1, 2) AS answer;
...@@ -175,12 +181,14 @@ SELECT add_em(1, 2) AS answer; ...@@ -175,12 +181,14 @@ SELECT add_em(1, 2) AS answer;
salary would be if it were doubled: salary would be if it were doubled:
<programlisting> <programlisting>
CREATE FUNCTION double_salary(EMP) RETURNS int4 CREATE FUNCTION double_salary(EMP)
AS 'SELECT $1.salary * 2 AS salary;' LANGUAGE 'sql'; RETURNS int4
AS 'SELECT $1.salary * 2 AS salary;'
LANGUAGE 'sql';
SELECT name, double_salary(EMP) AS dream SELECT name, double_salary(EMP) AS dream
FROM EMP FROM EMP
WHERE EMP.cubicle ~= '(2,1)'::point; WHERE EMP.cubicle ~= '(2,1)'::point;
+-----+-------+ +-----+-------+
...@@ -223,12 +231,13 @@ SELECT name(EMP) AS youngster ...@@ -223,12 +231,13 @@ SELECT name(EMP) AS youngster
that returns a single EMP instance: that returns a single EMP instance:
<programlisting> <programlisting>
CREATE FUNCTION new_emp() RETURNS EMP CREATE FUNCTION new_emp()
AS 'SELECT \'None\'::text AS name, RETURNS EMP
1000 AS salary, AS ' SELECT \'None\'::text AS name,
25 AS age, 1000 AS salary,
\'(2,2)\'::point AS cubicle' 25 AS age,
LANGUAGE 'sql'; \'(2,2)\'::point AS cubicle'
LANGUAGE 'sql';
</programlisting> </programlisting>
</para> </para>
<para> <para>
...@@ -303,10 +312,12 @@ NOTICE:parser: syntax error at or near "." ...@@ -303,10 +312,12 @@ NOTICE:parser: syntax error at or near "."
specified as the function's returntype. specified as the function's returntype.
<programlisting> <programlisting>
CREATE FUNCTION clean_EMP () RETURNS int4 CREATE FUNCTION clean_EMP ()
AS 'DELETE FROM EMP WHERE EMP.salary &lt;= 0; RETURNS int4
SELECT 1 AS ignore_this' AS ' DELETE FROM EMP
LANGUAGE 'sql'; WHERE EMP.salary &lt;= 0;
SELECT 1 AS ignore_this;'
LANGUAGE 'sql';
SELECT clean_EMP(); SELECT clean_EMP();
...@@ -837,8 +848,10 @@ str = (char *) GetAttributeByName(t, "name", &amp;isnull) ...@@ -837,8 +848,10 @@ str = (char *) GetAttributeByName(t, "name", &amp;isnull)
know about the c_overpaid function: know about the c_overpaid function:
<programlisting> <programlisting>
* CREATE FUNCTION c_overpaid(EMP, int4) RETURNS bool CREATE FUNCTION c_overpaid(EMP, int4)
AS '<replaceable>PGROOT</replaceable>/tutorial/obj/funcs.so' LANGUAGE 'c'; RETURNS bool
AS '<replaceable>PGROOT</replaceable>/tutorial/obj/funcs.so'
LANGUAGE 'c';
</programlisting> </programlisting>
</para> </para>
...@@ -999,7 +1012,7 @@ str = (char *) GetAttributeByName(t, "name", &amp;isnull) ...@@ -999,7 +1012,7 @@ str = (char *) GetAttributeByName(t, "name", &amp;isnull)
<para> <para>
For functions written in C, the SQL name declared in For functions written in C, the SQL name declared in
<command>CREATE FUNCTION</command> <command>CREATE FUNC TION</command>
must be exactly the same as the actual name of the function in the must be exactly the same as the actual name of the function in the
C code (hence it must be a legal C function name). C code (hence it must be a legal C function name).
</para> </para>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment