Database Reference
In-Depth Information
In our following example, we will implement a loop to iterate through a table and
return the results one by one using
return_next
:
warehouse_db=# CREATE OR REPLACE FUNCTION
getRecords_withperl_again()
RETURNS SETOF warehouse_tbl as $$
my $tblrow = spi_exec_query('SELECT warehouse_id,
warehouse_name, state FROM warehouse_tbl;');
my $numberofrows = $tblrow->{processed};
foreach my $rownumber (0 .. $numberofrows-1){
my $row = $tblrow->{rows}[$rownumber];
return_next($row);
}
return undef;
$$ LANGUAGE plperl;
For the last
return_next
statement, there should be
either the
return
or
return undef
statement.
Assuming that the
warehouse_tbl
table has a few rows, we will execute
this function on the
psql
command-line utility and observe its results in the
following manner:
warehouse_db=# SELECT warehouse_id, warehouse_name, state FROM
getRecords_withperl_again();
warehouse_id | warehouse_name | state
--------------+----------------+------
1 | Mark Corp | CT
(1 row)
PL/Tcl
Finally, PL/Tcl is last in our list of discussion for native procedural languages
of PostgreSQL. We will explore how to install it, handle arguments, and access
a database.
Installing PL/Tcl
Use the same
CREATE EXTENSION
method to create the PL/Tcl procedural language
in the following manner:
warehouse_db=# CREATE EXTENSION pltcl;