will assign a unique
name to each portal it creates. However, if you assign a non-null
string value to a cursor variable, that string will be used as its
portal name. This feature can be used as described in
<xref linkend="plpgsql-cursor-returning"/>.
</para>
<sect3 id="plpgsql-cursor-opening-open-for-query">
<title><command>OPEN FOR</command> <replaceable>query</replaceable></title>
<synopsis>
OPEN <replaceable>unbound_cursorvar</replaceable> <optional> <optional> NO </optional> SCROLL </optional> FOR <replaceable>query</replaceable>;
</synopsis>
<para>
The cursor variable is opened and given the specified query to
execute. The cursor cannot be open already, and it must have been
declared as an unbound cursor variable (that is, as a simple
<type>refcursor</type> variable). The query must be a
<command>SELECT</command>, or something else that returns rows
(such as <command>EXPLAIN</command>). The query
is treated in the same way as other SQL commands in
<application>PL/pgSQL</application>: <application>PL/pgSQL</application>
variable names are substituted, and the query plan is cached for
possible reuse. When a <application>PL/pgSQL</application>
variable is substituted into the cursor query, the value that is
substituted is the one it has at the time of the <command>OPEN</command>;
subsequent changes to the variable will not affect the cursor's
behavior.
The <literal>SCROLL</literal> and <literal>NO SCROLL</literal>
options have the same meanings as for a bound cursor.
</para>
<para>
An example:
<programlisting>
OPEN curs1 FOR SELECT * FROM foo WHERE key = mykey;
</programlisting>
</para>
</sect3>
<sect3 id="plpgsql-cursor-opening-open-for-execute">
<title><command>OPEN FOR EXECUTE</command></title>
<synopsis>
OPEN <replaceable>unbound_cursorvar</replaceable> <optional> <optional> NO </optional> SCROLL </optional> FOR EXECUTE <replaceable class="command">query_string</replaceable>
<optional> USING <replaceable>expression</replaceable> <optional>, ... </optional> </optional>;
</synopsis>
<para>
The cursor variable is opened and given the specified query to
execute. The cursor cannot be open already, and it must have been
declared as an unbound cursor variable (that is, as a simple
<type>refcursor</type> variable). The query is specified as a string
expression, in the same way as in the <command>EXECUTE</command>
command. As usual, this gives flexibility so the query plan can vary
from one run to the next (see <xref linkend="plpgsql-plan-caching"/>),
and it also means that variable substitution is not done on the
command string. As with <command>EXECUTE</command>, parameter values
can be inserted into the dynamic command via
<literal>format()</literal> and <literal>USING</literal>.
The <literal>SCROLL</literal> and
<literal>NO SCROLL</literal> options have the same meanings as for a bound
cursor.
</para>
<para>
An example:
<programlisting>
OPEN curs1 FOR EXECUTE format('SELECT * FROM %I WHERE col1 = $1',tabname) USING keyvalue;
</programlisting>
In this example, the table name is inserted into the query via
<function>format()</function>. The comparison value for <literal>col1</literal>
is inserted via a <literal>USING</literal> parameter, so it needs
no quoting.
</para>
</sect3>
<sect3 id="plpgsql-open-bound-cursor">
<title>Opening a Bound Cursor</title>
<synopsis>
OPEN <replaceable>bound_cursorvar</replaceable> <optional> ( <optional> <replaceable>argument_name</replaceable> { := | => } </optional> <replaceable>argument_value</replaceable>