Improve discussion of %TYPE and %ROWTYPE.

This commit is contained in:
Tom Lane 2001-05-11 06:10:44 +00:00
parent 1c1eb0fa0e
commit 0ad9abe72e
1 changed files with 18 additions and 16 deletions

View File

@ -1,5 +1,5 @@
<!-- <!--
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.29 2001/05/08 02:53:24 tgl Exp $ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.30 2001/05/11 06:10:44 tgl Exp $
--> -->
<chapter id="plpgsql"> <chapter id="plpgsql">
@ -396,12 +396,13 @@ user_id CONSTANT INTEGER := 10;
</sect3> </sect3>
<sect3 id="plpgsql-description-passed-vars"> <sect3 id="plpgsql-description-passed-vars">
<title>Variables Passed to Functions</title> <title>Parameters Passed to Functions</title>
<para> <para>
Variables passed to functions are named with the identifiers Parameters passed to functions are named with the identifiers
<literal>$1</literal>, <literal>$2</literal>, <literal>$1</literal>, <literal>$2</literal>,
etc. (maximum is 16). Some examples: etc. Optionally, aliases can be declared for the <literal>$n</literal>
parameter names for increased readability. Some examples:
<programlisting> <programlisting>
CREATE FUNCTION sales_tax(REAL) RETURNS REAL AS ' CREATE FUNCTION sales_tax(REAL) RETURNS REAL AS '
DECLARE DECLARE
@ -437,7 +438,7 @@ END;
<variablelist> <variablelist>
<varlistentry> <varlistentry>
<term> <term>
%TYPE <replaceable>variable</replaceable>%TYPE
</term> </term>
<listitem> <listitem>
<para> <para>
@ -447,9 +448,9 @@ END;
values. For example, let's say you have a column values. For example, let's say you have a column
named <type>user_id</type> in your named <type>user_id</type> in your
<type>users</type> table. To declare a variable with <type>users</type> table. To declare a variable with
the same datatype as users you do: the same datatype as users.user_id you write:
<programlisting> <programlisting>
user_id users.user_id%TYPE; user_id users.user_id%TYPE;
</programlisting> </programlisting>
</para> </para>
@ -467,30 +468,31 @@ user_id users.user_id%TYPE;
<varlistentry> <varlistentry>
<term> <term>
<replaceable>name</replaceable> <replaceable>table</replaceable>%ROWTYPE; <replaceable>table</replaceable>%ROWTYPE
</term> </term>
<listitem> <listitem>
<para> <para>
Declares a row with the structure of the given <type>%ROWTYPE</type> provides the composite datatype corresponding
table. <replaceable>table</replaceable> must be an existing to a whole row of the specified table.
<replaceable>table</replaceable> must be an existing
table or view name of the database. The fields of the row are table or view name of the database. The fields of the row are
accessed in the dot notation. Parameters to a function can be accessed in the dot notation. Parameters to a function can be
composite types (complete table rows). In that case, the composite types (complete table rows). In that case, the
corresponding identifier $n will be a rowtype, but it must be corresponding identifier $n will be a rowtype, and fields can
aliased using the ALIAS command described above. be selected from it, for example <literal>$1.user_id</literal>.
</para> </para>
<para> <para>
Only the user attributes of a table row are accessible in the Only the user-defined attributes of a table row are accessible in a
row, no OID or other system attributes (because the row could rowtype variable, not OID or other system attributes (because the
be from a view). The fields of the rowtype inherit the row could be from a view). The fields of the rowtype inherit the
table's field sizes or precision for <type>char()</type> table's field sizes or precision for <type>char()</type>
etc. data types. etc. data types.
</para> </para>
<programlisting> <programlisting>
DECLARE DECLARE
users_rec users%ROWTYPE; users_rec users%ROWTYPE;
user_id users%TYPE; user_id users.user_id%TYPE;
BEGIN BEGIN
user_id := users_rec.user_id; user_id := users_rec.user_id;
... ...