-
Variables Passed to Functions
+
Parameters Passed to Functions
- Variables passed to functions are named with the identifiers
+ Parameters passed to functions are named with the identifiers
$1, $2,
- etc. (maximum is 16). Some examples:
+ etc. Optionally, aliases can be declared for the $n
+ parameter names for increased readability. Some examples:
CREATE FUNCTION sales_tax(REAL) RETURNS REAL AS '
DECLARE
- %TYPE
+ variable%TYPE
values. For example, let's say you have a column
named user_id in your
users table. To declare a variable with
- the same datatype as users you do:
+ the same datatype as users.user_id you write:
-user_id users.user_id%TYPE;
+user_id users.user_id%TYPE;
- name table%ROWTYPE;
+ table%ROWTYPE
- Declares a row with the structure of the given
- table. table must be an existing
+ %ROWTYPE provides the composite datatype corresponding
+ to a whole row of the specified table.
+ table must be an existing
table or view name of the database. The fields of the row are
accessed in the dot notation. Parameters to a function can be
composite types (complete table rows). In that case, the
- corresponding identifier $n will be a rowtype, but it must be
- aliased using the ALIAS command described above.
+ corresponding identifier $n will be a rowtype, and fields can
+ be selected from it, for example $1.user_id.
- Only the user attributes of a table row are accessible in the
- row, no OID or other system attributes (because the row could
- be from a view). The fields of the rowtype inherit the
+ Only the user-defined attributes of a table row are accessible in a
+ rowtype variable, not OID or other system attributes (because the
+ row could be from a view). The fields of the rowtype inherit the
table's field sizes or precision for char()
etc. data types.
DECLARE
users_rec users%ROWTYPE;
- user_id users%TYPE;
+ user_id users.user_id%TYPE;
BEGIN
user_id := users_rec.user_id;
...