Home Explore Blog CI



postgresql

9th chunk of `doc/src/sgml/plpgsql.sgml`
5086fbd427a4bff7a1170cfdfbc972f4d2300deab551919c0000000100000fa0
 <replaceable>variable</replaceable>%TYPE
</synopsis>

   <para>
    <literal>%TYPE</literal> provides the data type of a table column
    or a previously-declared <application>PL/pgSQL</application>
    variable. You can use this to declare variables that will hold
    database values. For example, let's say you have a column named
    <literal>user_id</literal> in your <literal>users</literal>
    table. To declare a variable with the same data type as
    <literal>users.user_id</literal> you write:
<programlisting>
user_id users.user_id%TYPE;
</programlisting>
   </para>

   <para>
    It is also possible to write array decoration
    after <literal>%TYPE</literal>, thereby creating a variable that holds
    an array of the referenced type:
<programlisting>
user_ids users.user_id%TYPE[];
user_ids users.user_id%TYPE ARRAY[4];  -- equivalent to the above
</programlisting>
    Just as when declaring table columns that are arrays, it doesn't
    matter whether you write multiple bracket pairs or specific array
    dimensions: <productname>PostgreSQL</productname> treats all arrays of
    a given element type as the same type, regardless of dimensionality.
    (See <xref linkend="arrays-declaration"/>.)
   </para>

   <para>
    By using <literal>%TYPE</literal> you don't need to know the data
    type of the structure you are referencing, and most importantly,
    if the data type of the referenced item changes in the future (for
    instance: you change the type of <literal>user_id</literal>
    from <type>integer</type> to <type>real</type>), you might not need
    to change your function definition.
   </para>

   <para>
    <literal>%TYPE</literal> is particularly valuable in polymorphic
    functions, since the data types needed for internal variables can
    change from one call to the next.  Appropriate variables can be
    created by applying <literal>%TYPE</literal> to the function's
    arguments or result placeholders.
   </para>

  </sect2>

    <sect2 id="plpgsql-declaration-rowtypes">
     <title>Row Types</title>

<synopsis>
<replaceable>name</replaceable> <replaceable>table_name</replaceable><literal>%ROWTYPE</literal>;
<replaceable>name</replaceable> <replaceable>composite_type_name</replaceable>;
</synopsis>

   <para>
    A variable of a composite type is called a <firstterm>row</firstterm>
    variable (or <firstterm>row-type</firstterm> variable).  Such a variable
    can hold a whole row of a <command>SELECT</command> or <command>FOR</command>
    query result, so long as that query's column set matches the
    declared type of the variable.
    The individual fields of the row value
    are accessed using the usual dot notation, for example
    <literal>rowvar.field</literal>.
   </para>

   <para>
    A row variable can be declared to have the same type as the rows of
    an existing table or view, by using the
    <replaceable>table_name</replaceable><literal>%ROWTYPE</literal>
    notation; or it can be declared by giving a composite type's name.
    (Since every table has an associated composite type of the same name,
    it actually does not matter in <productname>PostgreSQL</productname> whether you
    write <literal>%ROWTYPE</literal> or not.  But the form with
    <literal>%ROWTYPE</literal> is more portable.)
   </para>

   <para>
    As with <literal>%TYPE</literal>, <literal>%ROWTYPE</literal> can be
    followed by array decoration to declare a variable that holds an array
    of the referenced composite type.
   </para>

   <para>
    Parameters to a function can be
    composite types (complete table rows). In that case, the
    corresponding identifier <literal>$<replaceable>n</replaceable></literal> will be a row variable, and fields can
    be selected from it, for example <literal>$1.user_id</literal>.
   </para>

   <para>
    Here is an example of using composite types.  <structname>table1</structname>
    and <structname>table2</structname> are existing tables having at least the
   

Title: PL/pgSQL: %TYPE for Data Type Copying and Row Types
Summary
%TYPE dynamically determines the data type of a table column or variable, ensuring type consistency and reducing maintenance when the referenced item's type changes. It's particularly useful in polymorphic functions. Row types, declared using table_name%ROWTYPE or composite_type_name, hold entire rows from queries and are accessed via dot notation (rowvar.field). They can also be declared as arrays. Function parameters can be composite types, with fields accessed via identifiers like $1.user_id.