gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

108 行
8.3KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>42.2. Structure of PL/pgSQL</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="plpgsql-overview.html" title="42.1. Overview" /><link rel="next" href="plpgsql-declarations.html" title="42.3. Declarations" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">42.2. Structure of <span xmlns="http://www.w3.org/1999/xhtml" class="application">PL/pgSQL</span></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plpgsql-overview.html" title="42.1. Overview">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plpgsql.html" title="Chapter 42. PL/pgSQL - SQL Procedural Language">Up</a></td><th width="60%" align="center">Chapter 42. <span xmlns="http://www.w3.org/1999/xhtml" class="application">PL/pgSQL</span> - <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">SQL</acronym> Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plpgsql-declarations.html" title="42.3. Declarations">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="PLPGSQL-STRUCTURE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">42.2. Structure of <span class="application">PL/pgSQL</span></h2></div></div></div><p>
  3. Functions written in <span class="application">PL/pgSQL</span> are defined
  4. to the server by executing <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a> commands.
  5. Such a command would normally look like, say,
  6. </p><pre class="programlisting">
  7. CREATE FUNCTION somefunc(integer, text) RETURNS integer
  8. AS '<em class="replaceable"><code>function body text</code></em>'
  9. LANGUAGE plpgsql;
  10. </pre><p>
  11. The function body is simply a string literal so far as <code class="command">CREATE
  12. FUNCTION</code> is concerned. It is often helpful to use dollar quoting
  13. (see <a class="xref" href="sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING" title="4.1.2.4. Dollar-Quoted String Constants">Section 4.1.2.4</a>) to write the function
  14. body, rather than the normal single quote syntax. Without dollar quoting,
  15. any single quotes or backslashes in the function body must be escaped by
  16. doubling them. Almost all the examples in this chapter use dollar-quoted
  17. literals for their function bodies.
  18. </p><p>
  19. <span class="application">PL/pgSQL</span> is a block-structured language.
  20. The complete text of a function body must be a
  21. <em class="firstterm">block</em>. A block is defined as:
  22. </p><pre class="synopsis">
  23. [<span class="optional"> &lt;&lt;<em class="replaceable"><code>label</code></em>&gt;&gt; </span>]
  24. [<span class="optional"> DECLARE
  25. <em class="replaceable"><code>declarations</code></em> </span>]
  26. BEGIN
  27. <em class="replaceable"><code>statements</code></em>
  28. END [<span class="optional"> <em class="replaceable"><code>label</code></em> </span>];
  29. </pre><p>
  30. </p><p>
  31. Each declaration and each statement within a block is terminated
  32. by a semicolon. A block that appears within another block must
  33. have a semicolon after <code class="literal">END</code>, as shown above;
  34. however the final <code class="literal">END</code> that
  35. concludes a function body does not require a semicolon.
  36. </p><div class="tip"><h3 class="title">Tip</h3><p>
  37. A common mistake is to write a semicolon immediately after
  38. <code class="literal">BEGIN</code>. This is incorrect and will result in a syntax error.
  39. </p></div><p>
  40. A <em class="replaceable"><code>label</code></em> is only needed if you want to
  41. identify the block for use
  42. in an <code class="literal">EXIT</code> statement, or to qualify the names of the
  43. variables declared in the block. If a label is given after
  44. <code class="literal">END</code>, it must match the label at the block's beginning.
  45. </p><p>
  46. All key words are case-insensitive.
  47. Identifiers are implicitly converted to lower case
  48. unless double-quoted, just as they are in ordinary SQL commands.
  49. </p><p>
  50. Comments work the same way in <span class="application">PL/pgSQL</span> code as in
  51. ordinary SQL. A double dash (<code class="literal">--</code>) starts a comment
  52. that extends to the end of the line. A <code class="literal">/*</code> starts a
  53. block comment that extends to the matching occurrence of
  54. <code class="literal">*/</code>. Block comments nest.
  55. </p><p>
  56. Any statement in the statement section of a block
  57. can be a <em class="firstterm">subblock</em>. Subblocks can be used for
  58. logical grouping or to localize variables to a small group
  59. of statements. Variables declared in a subblock mask any
  60. similarly-named variables of outer blocks for the duration
  61. of the subblock; but you can access the outer variables anyway
  62. if you qualify their names with their block's label. For example:
  63. </p><pre class="programlisting">
  64. CREATE FUNCTION somefunc() RETURNS integer AS $$
  65. &lt;&lt; outerblock &gt;&gt;
  66. DECLARE
  67. quantity integer := 30;
  68. BEGIN
  69. RAISE NOTICE 'Quantity here is %', quantity; -- Prints 30
  70. quantity := 50;
  71. --
  72. -- Create a subblock
  73. --
  74. DECLARE
  75. quantity integer := 80;
  76. BEGIN
  77. RAISE NOTICE 'Quantity here is %', quantity; -- Prints 80
  78. RAISE NOTICE 'Outer quantity here is %', outerblock.quantity; -- Prints 50
  79. END;
  80. RAISE NOTICE 'Quantity here is %', quantity; -- Prints 50
  81. RETURN quantity;
  82. END;
  83. $$ LANGUAGE plpgsql;
  84. </pre><p>
  85. </p><div class="note"><h3 class="title">Note</h3><p>
  86. There is actually a hidden <span class="quote">“<span class="quote">outer block</span>”</span> surrounding the body
  87. of any <span class="application">PL/pgSQL</span> function. This block provides the
  88. declarations of the function's parameters (if any), as well as some
  89. special variables such as <code class="literal">FOUND</code> (see
  90. <a class="xref" href="plpgsql-statements.html#PLPGSQL-STATEMENTS-DIAGNOSTICS" title="42.5.5. Obtaining the Result Status">Section 42.5.5</a>). The outer block is
  91. labeled with the function's name, meaning that parameters and special
  92. variables can be qualified with the function's name.
  93. </p></div><p>
  94. It is important not to confuse the use of
  95. <code class="command">BEGIN</code>/<code class="command">END</code> for grouping statements in
  96. <span class="application">PL/pgSQL</span> with the similarly-named SQL commands
  97. for transaction
  98. control. <span class="application">PL/pgSQL</span>'s <code class="command">BEGIN</code>/<code class="command">END</code>
  99. are only for grouping; they do not start or end a transaction.
  100. See <a class="xref" href="plpgsql-transactions.html" title="42.8. Transaction Management">Section 42.8</a> for information on managing
  101. transactions in <span class="application">PL/pgSQL</span>.
  102. Also, a block containing an <code class="literal">EXCEPTION</code> clause effectively
  103. forms a subtransaction that can be rolled back without affecting the
  104. outer transaction. For more about that see <a class="xref" href="plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING" title="42.6.8. Trapping Errors">Section 42.6.8</a>.
  105. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plpgsql-overview.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plpgsql.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plpgsql-declarations.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">42.1. Overview </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 42.3. Declarations</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1