Home Explore Blog CI



postgresql

13th chunk of `doc/src/sgml/plpgsql.sgml`
411c9e4b9aa9ef7241b81a36f0a3404ab36086dd2562b16d0000000100000fa5

    Anything not recognized as one of these statement types is presumed
    to be an SQL command and is sent to the main database engine to execute,
    as described in <xref linkend="plpgsql-statements-general-sql"/>.
   </para>

   <sect2 id="plpgsql-statements-assignment">
    <title>Assignment</title>

    <para>
     An assignment of a value to a <application>PL/pgSQL</application>
     variable is written as:
<synopsis>
<replaceable>variable</replaceable> { := | = } <replaceable>expression</replaceable>;
</synopsis>
     As explained previously, the expression in such a statement is evaluated
     by means of an SQL <command>SELECT</command> command sent to the main
     database engine.  The expression must yield a single value (possibly
     a row value, if the variable is a row or record variable).  The target
     variable can be a simple variable (optionally qualified with a block
     name), a field of a row or record target, or an element or slice of
     an array target.  Equal (<literal>=</literal>) can be
     used instead of PL/SQL-compliant <literal>:=</literal>.
    </para>

    <para>
     If the expression's result data type doesn't match the variable's
     data type, the value will be coerced as though by an assignment cast
     (see <xref linkend="typeconv-query"/>).  If no assignment cast is known
     for the pair of data types involved, the <application>PL/pgSQL</application>
     interpreter will attempt to convert the result value textually, that is
     by applying the result type's output function followed by the variable
     type's input function.  Note that this could result in run-time errors
     generated by the input function, if the string form of the result value
     is not acceptable to the input function.
    </para>

    <para>
     Examples:
<programlisting>
tax := subtotal * 0.06;
my_record.user_id := 20;
my_array[j] := 20;
my_array[1:3] := array[1,2,3];
complex_array[n].realpart = 12.3;
</programlisting>
    </para>
   </sect2>

   <sect2 id="plpgsql-statements-general-sql">
    <title>Executing SQL Commands</title>

    <para>
     In general, any SQL command that does not return rows can be executed
     within a <application>PL/pgSQL</application> function just by writing
     the command.  For example, you could create and fill a table by writing
<programlisting>
CREATE TABLE mytable (id int primary key, data text);
INSERT INTO mytable VALUES (1,'one'), (2,'two');
</programlisting>
    </para>

    <para>
     If the command does return rows (for example <command>SELECT</command>, or
     <command>INSERT</command>/<command>UPDATE</command>/<command>DELETE</command>/<command>MERGE</command>
     with <literal>RETURNING</literal>), there are two ways to proceed.
     When the command will return at most one row, or you only care about
     the first row of output, write the command as usual but add
     an <literal>INTO</literal> clause to capture the output, as described
     in <xref linkend="plpgsql-statements-sql-onerow"/>.
     To process all of the output rows, write the command as the data
     source for a <command>FOR</command> loop, as described in
     <xref linkend="plpgsql-records-iterating"/>.
    </para>

    <para>
     Usually it is not sufficient just to execute statically-defined SQL
     commands.  Typically you'll want a command to use varying data values,
     or even to vary in more fundamental ways such as by using different
     table names at different times.  Again, there are two ways to proceed
     depending on the situation.
    </para>

    <para>
     <application>PL/pgSQL</application> variable values can be
     automatically inserted into optimizable SQL commands, which
     are <command>SELECT</command>, <command>INSERT</command>,
     <command>UPDATE</command>, <command>DELETE</command>,
     <command>MERGE</command>, and certain
     utility commands that incorporate one of these, such
     as <command>EXPLAIN</command> and <command>CREATE TABLE

Title: PL/pgSQL: Assignment and Executing SQL Commands
Summary
PL/pgSQL handles variable assignment using := or =, evaluating the expression via a SELECT command. Type coercion is performed if the expression's result type doesn't match the variable's type, first attempting assignment cast and then textual conversion. Any SQL command not explicitly recognized by PL/pgSQL is sent to the main database engine for execution. SQL commands that return rows can be handled using INTO clauses (for single row or first row) or FOR loops (for processing all rows). Variable values can be automatically inserted into optimizable SQL commands.