Home Explore Blog CI



postgresql

5th chunk of `doc/src/sgml/plpgsql.sgml`
03a28078c422fb8e7db226ea5b713a74f6626acdd259ea100000000100000faa


    <para>
     All variables used in a block must be declared in the
     declarations section of the block.
     (The only exceptions are that the loop variable of a <literal>FOR</literal> loop
     iterating over a range of integer values is automatically declared as an
     integer variable, and likewise the loop variable of a <literal>FOR</literal> loop
     iterating over a cursor's result is automatically declared as a
     record variable.)
    </para>

    <para>
     <application>PL/pgSQL</application> variables can have any SQL data type, such as
     <type>integer</type>, <type>varchar</type>, and
     <type>char</type>.
    </para>

    <para>
     Here are some examples of variable declarations:
<programlisting>
user_id integer;
quantity numeric(5);
url varchar;
myrow tablename%ROWTYPE;
myfield tablename.columnname%TYPE;
arow RECORD;
</programlisting>
    </para>

    <para>
     The general syntax of a variable declaration is:
<synopsis>
<replaceable>name</replaceable> <optional> CONSTANT </optional> <replaceable>type</replaceable> <optional> COLLATE <replaceable>collation_name</replaceable> </optional> <optional> NOT NULL </optional> <optional> { DEFAULT | := | = } <replaceable>expression</replaceable> </optional>;
</synopsis>
      The <literal>DEFAULT</literal> clause, if given, specifies the initial value assigned
      to the variable when the block is entered.  If the <literal>DEFAULT</literal> clause
      is not given then the variable is initialized to the
      <acronym>SQL</acronym> null value.
      The <literal>CONSTANT</literal> option prevents the variable from being
      assigned to after initialization, so that its value will remain constant
      for the duration of the block.
      The <literal>COLLATE</literal> option specifies a collation to use for the
      variable (see <xref linkend="plpgsql-declaration-collation"/>).
      If <literal>NOT NULL</literal>
      is specified, an assignment of a null value results in a run-time
      error. All variables declared as <literal>NOT NULL</literal>
      must have a nonnull default value specified.
      Equal (<literal>=</literal>) can be used instead of PL/SQL-compliant
      <literal>:=</literal>.
     </para>

     <para>
      A variable's default value is evaluated and assigned to the variable
      each time the block is entered (not just once per function call).
      So, for example, assigning <literal>now()</literal> to a variable of type
      <type>timestamp</type> causes the variable to have the
      time of the current function call, not the time when the function was
      precompiled.
     </para>

     <para>
      Examples:
<programlisting>
quantity integer DEFAULT 32;
url varchar := 'http://mysite.com';
transaction_time CONSTANT timestamp with time zone := now();
</programlisting>
     </para>

     <para>
      Once declared, a variable's value can be used in later initialization
      expressions in the same block, for example:
<programlisting>
DECLARE
  x integer := 1;
  y integer := x + 1;
</programlisting>
     </para>

    <sect2 id="plpgsql-declaration-parameters">
     <title>Declaring Function Parameters</title>

     <para>
      Parameters passed to functions are named with the identifiers
      <literal>$1</literal>, <literal>$2</literal>,
      etc.  Optionally, aliases can be declared for
      <literal>$<replaceable>n</replaceable></literal>
      parameter names for increased readability.  Either the alias or the
      numeric identifier can then be used to refer to the parameter value.
     </para>

     <para>
      There are two ways to create an alias.  The preferred way is to give a
      name to the parameter in the <command>CREATE FUNCTION</command> command,
      for example:
<programlisting>
CREATE FUNCTION sales_tax(subtotal real) RETURNS real AS $$
BEGIN
    RETURN subtotal * 0.06;
END;
$$ LANGUAGE plpgsql;
</programlisting>
      The other way is to explicitly declare an alias, using the
      declaration

Title: PL/pgSQL Variable Declarations: Syntax, Defaults, and Parameters
Summary
PL/pgSQL requires all variables to be declared, except for loop variables in FOR loops. Variables can have any SQL data type. Examples of variable declarations are provided. The syntax for variable declaration includes name, optional CONSTANT, type, optional COLLATE, optional NOT NULL, and optional DEFAULT value. DEFAULT specifies the initial value; without it, the variable is initialized to NULL. CONSTANT prevents reassignment. COLLATE specifies collation. NOT NULL requires a non-null default value, and assigning NULL results in a runtime error. The default value is evaluated each time the block is entered. Variables can be used in later initialization expressions within the same block. Function parameters are named $1, $2, etc., but aliases can be declared for readability, either in the CREATE FUNCTION command or explicitly via a declaration.