From c99e851eea62717fcf32481db729dd71506e528a Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Sat, 20 May 2000 11:24:37 +0000 Subject: [PATCH] Clean up sql functions examples. --- doc/src/sgml/xfunc.sgml | 69 ++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/doc/src/sgml/xfunc.sgml b/doc/src/sgml/xfunc.sgml index 4a1c6c13a0..a6e97287b7 100644 --- a/doc/src/sgml/xfunc.sgml +++ b/doc/src/sgml/xfunc.sgml @@ -1,5 +1,5 @@ @@ -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: -create function TP1 (int4, float8) returns int4 - as 'update BANK set balance = BANK.balance - $2 - where BANK.acctountno = $1 - select(x = 1)' - language 'sql'; +CREATE FUNCTION tp1 (int4, float8) +RETURNS int4 +AS ' UPDATE bank + SET balance = bank.balance - $2 + WHERE bank.acctountno = $1; + SELECT 1;' +LANGUAGE 'sql'; A user could execute this function to debit account 17 by $100.00 as @@ -108,7 +110,7 @@ select (x = TP1( 17,100.0)); select function hobbies (EMP) returns set of HOBBIES - as 'select (HOBBIES.all) from HOBBIES + as 'select HOBBIES.* from HOBBIES where $1.name = HOBBIES.person' language 'sql'; @@ -123,8 +125,10 @@ select function hobbies (EMP) returns set of HOBBIES simply returns a base type, such as int4: -CREATE FUNCTION one() RETURNS int4 - AS 'SELECT 1 as RESULT' LANGUAGE 'sql'; +CREATE FUNCTION one() +RETURNS int4 +AS 'SELECT 1 as RESULT;' +LANGUAGE 'sql'; SELECT one() AS answer; @@ -149,8 +153,10 @@ SELECT one() AS answer; and $2: -CREATE FUNCTION add_em(int4, int4) RETURNS int4 - AS 'SELECT $1 + $2;' LANGUAGE 'sql'; +CREATE FUNCTION add_em(int4, int4) +RETURNS int4 +AS 'SELECT $1 + $2;' +LANGUAGE 'sql'; 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: -CREATE FUNCTION double_salary(EMP) RETURNS int4 - AS 'SELECT $1.salary * 2 AS salary;' LANGUAGE 'sql'; +CREATE FUNCTION double_salary(EMP) +RETURNS int4 +AS 'SELECT $1.salary * 2 AS salary;' +LANGUAGE 'sql'; SELECT name, double_salary(EMP) AS dream - FROM EMP - WHERE EMP.cubicle ~= '(2,1)'::point; +FROM EMP +WHERE EMP.cubicle ~= '(2,1)'::point; +-----+-------+ @@ -223,12 +231,13 @@ SELECT name(EMP) AS youngster that returns a single EMP instance: -CREATE FUNCTION new_emp() RETURNS EMP - AS 'SELECT \'None\'::text AS name, - 1000 AS salary, - 25 AS age, - \'(2,2)\'::point AS cubicle' - LANGUAGE 'sql'; +CREATE FUNCTION new_emp() +RETURNS EMP +AS ' SELECT \'None\'::text AS name, + 1000 AS salary, + 25 AS age, + \'(2,2)\'::point AS cubicle' +LANGUAGE 'sql'; @@ -303,10 +312,12 @@ NOTICE:parser: syntax error at or near "." specified as the function's returntype. -CREATE FUNCTION clean_EMP () RETURNS int4 - AS 'DELETE FROM EMP WHERE EMP.salary <= 0; -SELECT 1 AS ignore_this' - LANGUAGE 'sql'; +CREATE FUNCTION clean_EMP () +RETURNS int4 +AS ' DELETE FROM EMP + WHERE EMP.salary <= 0; + SELECT 1 AS ignore_this;' +LANGUAGE 'sql'; SELECT clean_EMP(); @@ -837,8 +848,10 @@ str = (char *) GetAttributeByName(t, "name", &isnull) know about the c_overpaid function: -* CREATE FUNCTION c_overpaid(EMP, int4) RETURNS bool - AS 'PGROOT/tutorial/obj/funcs.so' LANGUAGE 'c'; +CREATE FUNCTION c_overpaid(EMP, int4) +RETURNS bool +AS 'PGROOT/tutorial/obj/funcs.so' +LANGUAGE 'c'; @@ -999,7 +1012,7 @@ str = (char *) GetAttributeByName(t, "name", &isnull) For functions written in C, the SQL name declared in - CREATE FUNCTION + CREATE FUNC TION 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). -- 2.40.0