Database Reference
In-Depth Information
RETURN total;
END;
$$ LANGUAGE plpgsql;
Assuming that the warehouse_tbl table contains a few rows, launch the psql utility,
connect to the warehouse_db database, and start the execution of the preceding code
from scratch in the following manner:
./psql -U postgres -d warehouse_db -p 5432
Here, the postgres keyword is the default user and 5432 is the default port for a
PostgreSQL installation.
Now, we can create the getRecords() function using the following statement:
warehouse_db=# CREATE OR REPLACE FUNCTION getRecords()
RETURNS INTEGER AS $$
DECLARE
total INTEGER;
BEGIN
SELECT COUNT(*) INTO total FROM warehouse_tbl;
RETURN total;
END;
$$ LANGUAGE plpgsql;
The function has been created; now call it to see the result using the
following statement:
warehouse_db=# SELECT getRecords();
getRecords
------------
1
(1 row)
Using comments in PL/pgSQL
Comments can be used in the same way in PL/pgSQL as in other languages. You can
either use single-line comments, such as starting with two dashes and with no end
character, as shown in the following format:
- - single line comment style
Otherwise, use the multiline style, such as the following format:
/* comments here */ for multi line comments block i.e.
/*
 
Search WWH ::




Custom Search