So far, I've found this to be a good language to use. But it can be
difficult to get started. A lack of practical examples makes things tough. I
did manage to find a few examples and have provided them here.
CREATE FUNCTION ct1(text, text) RETURNS text AS '
BEGIN
RETURN $1 || $2;
END;
' LANGUAGE 'plpgsql';
This function, ct1, takes two strings and concatenates them. Here's how I saved
that function and then used it:
[dan@xeon:~] $ /usr/local/pgsql/bin/psql FreshPorts2
Welcome to psql, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit
FreshPorts2=# CREATE FUNCTION ct1(text, text) RETURNS text AS '
FreshPorts2'# BEGIN
FreshPorts2'# RETURN $1 || $2;
FreshPorts2'# END;
FreshPorts2'# ' LANGUAGE 'plpgsql';
CREATE
FreshPorts2=# select ct1('abc', 'def');
ct1
--------
abcdef
(1 row)
FreshPorts2=#
If you decide to modify this function, you need to first drop it, then save it.
Here's the message you'll see if you don't drop it first:
ERROR: ProcedureCreate: procedure ct1 already exists with same
arguments
This is the command you use to drop that function:
drop function ct1(text, text);
Or, you could use the CREATE OR REPLACE FUNCTION phrase
instead of CREATE FUNCTION..
Functions can be overloaded. That means you could have another function ct1 which
takes different arguments.
You can see other example in Chapter 10 of the PostgreSQL User's Guide. |