converted to lower case
unless double-quoted, just as they are in ordinary SQL commands.
</para>
<para>
Comments work the same way in <application>PL/pgSQL</application> code as in
ordinary SQL. A double dash (<literal>--</literal>) starts a comment
that extends to the end of the line. A <literal>/*</literal> starts a
block comment that extends to the matching occurrence of
<literal>*/</literal>. Block comments nest.
</para>
<para>
Any statement in the statement section of a block
can be a <firstterm>subblock</firstterm>. Subblocks can be used for
logical grouping or to localize variables to a small group
of statements. Variables declared in a subblock mask any
similarly-named variables of outer blocks for the duration
of the subblock; but you can access the outer variables anyway
if you qualify their names with their block's label. For example:
<programlisting>
CREATE FUNCTION somefunc() RETURNS integer AS $$
<< outerblock >>
DECLARE
quantity integer := 30;
BEGIN
RAISE NOTICE 'Quantity here is %', quantity; -- Prints 30
quantity := 50;
--
-- Create a subblock
--
DECLARE
quantity integer := 80;
BEGIN
RAISE NOTICE 'Quantity here is %', quantity; -- Prints 80
RAISE NOTICE 'Outer quantity here is %', outerblock.quantity; -- Prints 50
END;
RAISE NOTICE 'Quantity here is %', quantity; -- Prints 50
RETURN quantity;
END;
$$ LANGUAGE plpgsql;
</programlisting>
</para>
<note>
<para>
There is actually a hidden <quote>outer block</quote> surrounding the body
of any <application>PL/pgSQL</application> function. This block provides the
declarations of the function's parameters (if any), as well as some
special variables such as <literal>FOUND</literal> (see
<xref linkend="plpgsql-statements-diagnostics"/>). The outer block is
labeled with the function's name, meaning that parameters and special
variables can be qualified with the function's name.
</para>
</note>
<para>
It is important not to confuse the use of
<command>BEGIN</command>/<command>END</command> for grouping statements in
<application>PL/pgSQL</application> with the similarly-named SQL commands
for transaction
control. <application>PL/pgSQL</application>'s <command>BEGIN</command>/<command>END</command>
are only for grouping; they do not start or end a transaction.
See <xref linkend="plpgsql-transactions"/> for information on managing
transactions in <application>PL/pgSQL</application>.
Also, a block containing an <literal>EXCEPTION</literal> clause effectively
forms a subtransaction that can be rolled back without affecting the
outer transaction. For more about that see <xref
linkend="plpgsql-error-trapping"/>.
</para>
</sect1>
<sect1 id="plpgsql-declarations">
<title>Declarations</title>
<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>