To be able to create a function, you must have <literal>USAGE</literal>
privilege on the argument types and the return type.
</para>
+
+ <para>
+ Refer to <xref linkend="xfunc"/> for further information on writing
+ functions.
+ </para>
</refsect1>
<refsect1>
</varlistentry>
</variablelist>
-
- <para>
- Refer to <xref linkend="xfunc"/> for further information on writing
- functions.
- </para>
-
</refsect1>
<refsect1 id="sql-createfunction-overloading">
<title>Examples</title>
<para>
- Here are some trivial examples to help you get started. For more
- information and examples, see <xref linkend="xfunc"/>.
+ Add two integers using a SQL function:
<programlisting>
CREATE FUNCTION add(integer, integer) RETURNS integer
AS 'select $1 + $2;'
</indexterm>
<para>
- A procedure is a database object similar to a function. The difference is
- that a procedure does not return a value, so there is no return type
- declaration. While a function is called as part of a query or DML
- command, a procedure is called in isolation using
- the <xref linkend="sql-call"/> command. If the CALL command is not
- part of an explicit transaction, a procedure in many server-side
- languages can commit, rollback, and begin new transactions during
- its execution, which is not possible in functions.
+ A procedure is a database object similar to a function.
+ The key differences are:
+
+ <itemizedlist>
+ <listitem>
+ <para>
+ Procedures are defined with the <xref linkend="sql-createprocedure"/>
+ command, not <command>CREATE FUNCTION</command>.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ Procedures do not return a function value; hence <command>CREATE
+ PROCEDURE</command> lacks a <literal>RETURNS</literal> clause.
+ However, procedures can instead return data to their callers via
+ output parameters.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ While a function is called as part of a query or DML command, a
+ procedure is called in isolation using
+ the <xref linkend="sql-call"/> command.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ A procedure can commit or roll back transactions during its
+ execution (then automatically beginning a new transaction), so long
+ as the invoking <command>CALL</command> command is not part of an
+ explicit transaction block. A function cannot do that.
+ </para>
+ </listitem>
+ <listitem>
+ <para>
+ Certain function attributes, such as strictness, don't apply to
+ procedures. Those attributes control how the function is
+ used in a query, which isn't relevant to procedures.
+ </para>
+ </listitem>
+ </itemizedlist>
</para>
<para>
- The explanations on how to define user-defined functions in the rest of
- this chapter apply to procedures as well, except that
- the <xref linkend="sql-createprocedure"/> command is used instead, there is
- no return type, and some other features such as strictness don't apply.
+ The explanations in the following sections about how to define
+ user-defined functions apply to procedures as well, except for the
+ points made above.
</para>
<para>