Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > media > main-testing > by-pkgid > bab02a23fa9f3df8d66a9a3231b50245 > files > 758

postgresql8.3-docs-8.3.6-2mdv2008.1.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>DECLARE</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 8.3.6 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="DEALLOCATE"
HREF="sql-deallocate.html"><LINK
REL="NEXT"
TITLE="DELETE"
HREF="sql-delete.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2009-02-03T04:34:16"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
>PostgreSQL 8.3.6 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-deallocate.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-deallocate.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-delete.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-delete.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-DECLARE"
></A
>DECLARE</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN55587"
></A
><H2
>Name</H2
>DECLARE&nbsp;--&nbsp;define a cursor</DIV
><A
NAME="AEN55590"
></A
><A
NAME="AEN55592"
></A
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN55595"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>DECLARE <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> [ BINARY ] [ INSENSITIVE ] [ [ NO ] SCROLL ]
    CURSOR [ { WITH | WITHOUT } HOLD ] FOR <TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
></PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN55599"
></A
><H2
>Description</H2
><P
>   <TT
CLASS="COMMAND"
>DECLARE</TT
> allows a user to create cursors, which
   can be used to retrieve
   a small number of rows at a time out of a larger query.
   After the cursor is created, rows are fetched from it using
   <A
HREF="sql-fetch.html"
><I
>FETCH</I
></A
>.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN55604"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
></DT
><DD
><P
>      The name of the cursor to be created.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>BINARY</TT
></DT
><DD
><P
>      Causes the cursor to return data in binary rather than in text format.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>INSENSITIVE</TT
></DT
><DD
><P
>      Indicates that data retrieved from the cursor should be
      unaffected by updates to the table(s) underlying the cursor that occur
      after the cursor is created.  In <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>,
      this is the default behavior; so this key word has no
      effect and is only accepted for compatibility with the SQL standard.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>SCROLL</TT
><BR><TT
CLASS="LITERAL"
>NO SCROLL</TT
></DT
><DD
><P
>      <TT
CLASS="LITERAL"
>SCROLL</TT
> specifies that the cursor can be used
      to retrieve rows in a nonsequential fashion (e.g.,
      backward). Depending upon the complexity of the query's
      execution plan, specifying <TT
CLASS="LITERAL"
>SCROLL</TT
> might impose
      a performance penalty on the query's execution time.
      <TT
CLASS="LITERAL"
>NO SCROLL</TT
> specifies that the cursor cannot be
      used to retrieve rows in a nonsequential fashion.  The default is to
      allow scrolling in some cases; this is not the same as specifying
      <TT
CLASS="LITERAL"
>SCROLL</TT
>. See <A
HREF="sql-declare.html#SQL-DECLARE-NOTES"
><I
>Notes</I
></A
> for details.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>WITH HOLD</TT
><BR><TT
CLASS="LITERAL"
>WITHOUT HOLD</TT
></DT
><DD
><P
>      <TT
CLASS="LITERAL"
>WITH HOLD</TT
> specifies that the cursor can
      continue to be used after the transaction that created it
      successfully commits.  <TT
CLASS="LITERAL"
>WITHOUT HOLD</TT
> specifies
      that the cursor cannot be used outside of the transaction that
      created it. If neither <TT
CLASS="LITERAL"
>WITHOUT HOLD</TT
> nor
      <TT
CLASS="LITERAL"
>WITH HOLD</TT
> is specified, <TT
CLASS="LITERAL"
>WITHOUT
      HOLD</TT
> is the default.
     </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>query</I
></TT
></DT
><DD
><P
>      A <A
HREF="sql-select.html"
><I
>SELECT</I
></A
> or
      <A
HREF="sql-values.html"
><I
>VALUES</I
></A
> command
      which will provide the rows to be returned by the cursor.
     </P
></DD
></DL
></DIV
><P
>   The key words <TT
CLASS="LITERAL"
>BINARY</TT
>,
   <TT
CLASS="LITERAL"
>INSENSITIVE</TT
>, and <TT
CLASS="LITERAL"
>SCROLL</TT
> can
   appear in any order.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="SQL-DECLARE-NOTES"
></A
><H2
>Notes</H2
><P
>   Normal cursors return data in text format, the same as a
   <TT
CLASS="COMMAND"
>SELECT</TT
> would produce.  The <TT
CLASS="LITERAL"
>BINARY</TT
> option
   specifies that the cursor should return data in binary format.
   This reduces conversion effort for both the server and client,
   at the cost of more programmer effort to deal with platform-dependent
   binary data formats.
   As an example, if a query returns a value of one from an integer column,
   you would get a string of <TT
CLASS="LITERAL"
>1</TT
> with a default cursor,
   whereas with a binary cursor you would get
   a 4-byte field containing the internal representation of the value
   (in big-endian byte order).
  </P
><P
>   Binary cursors should be used carefully.  Many applications,
   including <SPAN
CLASS="APPLICATION"
>psql</SPAN
>, are not prepared to
   handle binary cursors and expect data to come back in the text
   format.
  </P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>    When the client application uses the <SPAN
CLASS="QUOTE"
>"extended query"</SPAN
> protocol
    to issue a <TT
CLASS="COMMAND"
>FETCH</TT
> command, the Bind protocol message
    specifies whether data is to be retrieved in text or binary format.
    This choice overrides the way that the cursor is defined.  The concept
    of a binary cursor as such is thus obsolete when using extended query
    protocol &mdash; any cursor can be treated as either text or binary.
   </P
></BLOCKQUOTE
></DIV
><P
>    Unless <TT
CLASS="LITERAL"
>WITH HOLD</TT
> is specified, the cursor
    created by this command can only be used within the current
    transaction.  Thus, <TT
CLASS="COMMAND"
>DECLARE</TT
> without <TT
CLASS="LITERAL"
>WITH
    HOLD</TT
> is useless outside a transaction block: the cursor would
    survive only to the completion of the statement.  Therefore
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> reports an error if such a
    command is used outside a transaction block.
    Use
    <A
HREF="sql-begin.html"
><I
>BEGIN</I
></A
>,
    <A
HREF="sql-commit.html"
><I
>COMMIT</I
></A
>
    and
    <A
HREF="sql-rollback.html"
><I
>ROLLBACK</I
></A
>
    to define a transaction block.
   </P
><P
>    If <TT
CLASS="LITERAL"
>WITH HOLD</TT
> is specified and the transaction
    that created the cursor successfully commits, the cursor can
    continue to be accessed by subsequent transactions in the same
    session.  (But if the creating transaction is aborted, the cursor
    is removed.)  A cursor created with <TT
CLASS="LITERAL"
>WITH HOLD</TT
>
    is closed when an explicit <TT
CLASS="COMMAND"
>CLOSE</TT
> command is
    issued on it, or the session ends.  In the current implementation,
    the rows represented by a held cursor are copied into a temporary
    file or memory area so that they remain available for subsequent
    transactions.
   </P
><P
>    <TT
CLASS="LITERAL"
>WITH HOLD</TT
> may not be specified when the query
    includes <TT
CLASS="LITERAL"
>FOR UPDATE</TT
> or <TT
CLASS="LITERAL"
>FOR SHARE</TT
>.
   </P
><P
>    The <TT
CLASS="LITERAL"
>SCROLL</TT
> option should be specified when defining a
    cursor that will be used to fetch backwards.  This is required by
    the SQL standard.  However, for compatibility with earlier
    versions, <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> will allow
    backward fetches without <TT
CLASS="LITERAL"
>SCROLL</TT
>, if the cursor's query
    plan is simple enough that no extra overhead is needed to support
    it. However, application developers are advised not to rely on
    using backward fetches from a cursor that has not been created
    with <TT
CLASS="LITERAL"
>SCROLL</TT
>.  If <TT
CLASS="LITERAL"
>NO SCROLL</TT
> is
    specified, then backward fetches are disallowed in any case.
   </P
><P
>    If the cursor's query includes <TT
CLASS="LITERAL"
>FOR UPDATE</TT
> or <TT
CLASS="LITERAL"
>FOR
    SHARE</TT
>, then returned rows are locked at the time they are first
    fetched, in the same way as for a regular
    <A
HREF="sql-select.html"
><I
>SELECT</I
></A
> command with
    these options.
    In addition, the returned rows will be the most up-to-date versions;
    therefore these options provide the equivalent of what the SQL standard
    calls a <SPAN
CLASS="QUOTE"
>"sensitive cursor"</SPAN
>.  It is often wise to use <TT
CLASS="LITERAL"
>FOR
    UPDATE</TT
> if the cursor is intended to be used with <TT
CLASS="COMMAND"
>UPDATE
    ... WHERE CURRENT OF</TT
> or <TT
CLASS="COMMAND"
>DELETE ... WHERE CURRENT OF</TT
>,
    since this will prevent other sessions from changing the rows between
    the time they are fetched and the time they are updated.  Without
    <TT
CLASS="LITERAL"
>FOR UPDATE</TT
>, a subsequent <TT
CLASS="LITERAL"
>WHERE CURRENT OF</TT
> command
    will have no effect if the row was changed meanwhile.
   </P
><P
>    <TT
CLASS="LITERAL"
>SCROLL</TT
> may not be specified when the query
    includes <TT
CLASS="LITERAL"
>FOR UPDATE</TT
> or <TT
CLASS="LITERAL"
>FOR SHARE</TT
>.
   </P
><P
>    The SQL standard only makes provisions for cursors in embedded
    <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
>.  The <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
    server does not implement an <TT
CLASS="COMMAND"
>OPEN</TT
> statement for
    cursors; a cursor is considered to be open when it is declared.
    However, <SPAN
CLASS="APPLICATION"
>ECPG</SPAN
>, the embedded SQL
    preprocessor for <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>, supports
    the standard SQL cursor conventions, including those involving
    <TT
CLASS="COMMAND"
>DECLARE</TT
> and <TT
CLASS="COMMAND"
>OPEN</TT
> statements.
   </P
><P
>    You can see all available cursors by querying the <A
HREF="view-pg-cursors.html"
><TT
CLASS="STRUCTNAME"
>pg_cursors</TT
></A
>
    system view.
   </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN55717"
></A
><H2
>Examples</H2
><P
>   To declare a cursor:
</P><PRE
CLASS="PROGRAMLISTING"
>DECLARE liahona CURSOR FOR SELECT * FROM films;</PRE
><P>
   See <A
HREF="sql-fetch.html"
><I
>FETCH</I
></A
> for more
   examples of cursor usage.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN55722"
></A
><H2
>Compatibility</H2
><P
>   The SQL standard says that it is implementation-dependent whether cursors
   are sensitive to concurrent updates of the underlying data by default.  In
   <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>, cursors are insensitive by default,
   and can be made sensitive by specifying <TT
CLASS="LITERAL"
>FOR UPDATE</TT
>.  Other
   products may work differently.
  </P
><P
>   The SQL standard allows cursors only in embedded
   <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> and in modules. <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
   permits cursors to be used interactively.
  </P
><P
>   Binary cursors are a <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
   extension.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN55732"
></A
><H2
>See Also</H2
><A
HREF="sql-close.html"
><I
>CLOSE</I
></A
>, <A
HREF="sql-fetch.html"
><I
>FETCH</I
></A
>, <A
HREF="sql-move.html"
><I
>MOVE</I
></A
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-deallocate.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-delete.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>DEALLOCATE</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>DELETE</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>