Database Reference
In-Depth Information
Writing functions in PL/Tcl
The overall syntactical structure remains the same, except with the function body
being the Tcl code. This is as follows:
CREATE OR REPLACE FUNCTION name (arguments)
RETURNS return-type AS $$
# PL/Tcl code
$$ LANGUAGE plperl;
Why don't you try writing a function using the preceding syntax. The test_tcl()
function will simply return the a variable that is initially assigned to a value using
the Tcl syntax in the following manner:
warehouse_db=# CREATE OR REPLACE FUNCTION test_tcl()
RETURNS text AS $$
set a "PostgreSQL Rocks."
return $a;
$$ LANGUAGE pltcl;
The result can be seen using the following statement:
warehouse_db=# SELECT test_tcl();
test_tcl
------------------
PostgreSQL Rocks.
(1 row)
Handling arguments with PL/Tcl
The tcl_arguments() function computes the sum of three variables passed as
arguments. These variables are assigned to the arg variable as a list and iterated
using foreach . One by one, each argument is added to get the total result. This can
be done in the following manner:
warehouse_db=# CREATE OR REPLACE FUNCTION tcl_arguments(integer,
integer, integer)
RETURNS integer AS $$
set arg [list $1 $2 $3]
set plus 0
foreach variable $arg {
set plus [expr $plus + $variable]
}
return $plus
$$ LANGUAGE pltcl;
 
Search WWH ::




Custom Search