Home Explore Blog CI



postgresql

17th chunk of `doc/src/sgml/syntax.sgml`
4eea125dde68b0c2ef7012c04a1992cdb8863d9fe38ac9f80000000100000fb4
 </indexterm>

   <indexterm>
    <primary>$</primary>
   </indexterm>

   <para>
    A positional parameter reference is used to indicate a value
    that is supplied externally to an SQL statement.  Parameters are
    used in SQL function definitions and in prepared queries.  Some
    client libraries also support specifying data values separately
    from the SQL command string, in which case parameters are used to
    refer to the out-of-line data values.
    The form of a parameter reference is:
<synopsis>
$<replaceable>number</replaceable>
</synopsis>
   </para>

   <para>
    For example, consider the definition of a function,
    <function>dept</function>, as:

<programlisting>
CREATE FUNCTION dept(text) RETURNS dept
    AS $$ SELECT * FROM dept WHERE name = $1 $$
    LANGUAGE SQL;
</programlisting>

    Here the <literal>$1</literal> references the value of the first
    function argument whenever the function is invoked.
   </para>
  </sect2>

  <sect2 id="sql-expressions-subscripts">
   <title>Subscripts</title>

   <indexterm>
    <primary>subscript</primary>
   </indexterm>

   <para>
    If an expression yields a value of an array type, then a specific
    element of the array value can be extracted by writing
<synopsis>
<replaceable>expression</replaceable>[<replaceable>subscript</replaceable>]
</synopsis>
    or multiple adjacent elements (an <quote>array slice</quote>) can be extracted
    by writing
<synopsis>
<replaceable>expression</replaceable>[<replaceable>lower_subscript</replaceable>:<replaceable>upper_subscript</replaceable>]
</synopsis>
    (Here, the brackets <literal>[ ]</literal> are meant to appear literally.)
    Each <replaceable>subscript</replaceable> is itself an expression,
    which will be rounded to the nearest integer value.
   </para>

   <para>
    In general the array <replaceable>expression</replaceable> must be
    parenthesized, but the parentheses can be omitted when the expression
    to be subscripted is just a column reference or positional parameter.
    Also, multiple subscripts can be concatenated when the original array
    is multidimensional.
    For example:

<programlisting>
mytable.arraycolumn[4]
mytable.two_d_column[17][34]
$1[10:42]
(arrayfunction(a,b))[42]
</programlisting>

    The parentheses in the last example are required.
    See <xref linkend="arrays"/> for more about arrays.
   </para>
  </sect2>

  <sect2 id="field-selection">
   <title>Field Selection</title>

   <indexterm>
    <primary>field selection</primary>
   </indexterm>

   <para>
    If an expression yields a value of a composite type (row type), then a
    specific field of the row can be extracted by writing
<synopsis>
<replaceable>expression</replaceable>.<replaceable>fieldname</replaceable>
</synopsis>
   </para>

   <para>
    In general the row <replaceable>expression</replaceable> must be
    parenthesized, but the parentheses can be omitted when the expression
    to be selected from is just a table reference or positional parameter.
    For example:

<programlisting>
mytable.mycolumn
$1.somecolumn
(rowfunction(a,b)).col3
</programlisting>

    (Thus, a qualified column reference is actually just a special case
    of the field selection syntax.)  An important special case is
    extracting a field from a table column that is of a composite type:

<programlisting>
(compositecol).somefield
(mytable.compositecol).somefield
</programlisting>

    The parentheses are required here to show that
    <structfield>compositecol</structfield> is a column name not a table name,
    or that <structname>mytable</structname> is a table name not a schema name
    in the second case.
   </para>

   <para>
    You can ask for all fields of a composite value by
    writing <literal>.*</literal>:
<programlisting>
(compositecol).*
</programlisting>
    This notation behaves differently depending on context;
    see <xref linkend="rowtypes-usage"/> for details.
   </para>
  </sect2>

  <sect2 id="sql-expressions-operator-calls">

Title: SQL Expressions: Subscripts and Field Selection
Summary
This section delves into subscripts and field selection within SQL expressions. It describes how to extract a specific element or a slice from an array using subscripts and how to access a specific field from a composite type (row type) using field selection. The use of parentheses around expressions is highlighted, along with examples to illustrate the syntax.