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

169 行
15KB

  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>CREATE PROCEDURE</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="sql-createpolicy.html" title="CREATE POLICY" /><link rel="next" href="sql-createpublication.html" title="CREATE PUBLICATION" /></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">CREATE PROCEDURE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createpolicy.html" title="CREATE POLICY">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</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="sql-createpublication.html" title="CREATE PUBLICATION">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATEPROCEDURE"><div class="titlepage"></div><a id="id-1.9.3.76.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE PROCEDURE</span></h2><p>CREATE PROCEDURE — define a new procedure</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CREATE [ OR REPLACE ] PROCEDURE
  4. <em class="replaceable"><code>name</code></em> ( [ [ <em class="replaceable"><code>argmode</code></em> ] [ <em class="replaceable"><code>argname</code></em> ] <em class="replaceable"><code>argtype</code></em> [ { DEFAULT | = } <em class="replaceable"><code>default_expr</code></em> ] [, ...] ] )
  5. { LANGUAGE <em class="replaceable"><code>lang_name</code></em>
  6. | TRANSFORM { FOR TYPE <em class="replaceable"><code>type_name</code></em> } [, ... ]
  7. | [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
  8. | SET <em class="replaceable"><code>configuration_parameter</code></em> { TO <em class="replaceable"><code>value</code></em> | = <em class="replaceable"><code>value</code></em> | FROM CURRENT }
  9. | AS '<em class="replaceable"><code>definition</code></em>'
  10. | AS '<em class="replaceable"><code>obj_file</code></em>', '<em class="replaceable"><code>link_symbol</code></em>'
  11. } ...
  12. </pre></div><div class="refsect1" id="SQL-CREATEPROCEDURE-DESCRIPTION"><h2>Description</h2><p>
  13. <code class="command">CREATE PROCEDURE</code> defines a new procedure.
  14. <code class="command">CREATE OR REPLACE PROCEDURE</code> will either create a
  15. new procedure, or replace an existing definition.
  16. To be able to define a procedure, the user must have the
  17. <code class="literal">USAGE</code> privilege on the language.
  18. </p><p>
  19. If a schema name is included, then the procedure is created in the
  20. specified schema. Otherwise it is created in the current schema.
  21. The name of the new procedure must not match any existing procedure or function
  22. with the same input argument types in the same schema. However,
  23. procedures and functions of different argument types can share a name (this is
  24. called <em class="firstterm">overloading</em>).
  25. </p><p>
  26. To replace the current definition of an existing procedure, use
  27. <code class="command">CREATE OR REPLACE PROCEDURE</code>. It is not possible
  28. to change the name or argument types of a procedure this way (if you
  29. tried, you would actually be creating a new, distinct procedure).
  30. </p><p>
  31. When <code class="command">CREATE OR REPLACE PROCEDURE</code> is used to replace an
  32. existing procedure, the ownership and permissions of the procedure
  33. do not change. All other procedure properties are assigned the
  34. values specified or implied in the command. You must own the procedure
  35. to replace it (this includes being a member of the owning role).
  36. </p><p>
  37. The user that creates the procedure becomes the owner of the procedure.
  38. </p><p>
  39. To be able to create a procedure, you must have <code class="literal">USAGE</code>
  40. privilege on the argument types.
  41. </p></div><div class="refsect1" id="id-1.9.3.76.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  42. The name (optionally schema-qualified) of the procedure to create.
  43. </p></dd><dt><span class="term"><em class="replaceable"><code>argmode</code></em></span></dt><dd><p>
  44. The mode of an argument: <code class="literal">IN</code>,
  45. <code class="literal">INOUT</code>, or <code class="literal">VARIADIC</code>. If omitted,
  46. the default is <code class="literal">IN</code>. (<code class="literal">OUT</code>
  47. arguments are currently not supported for procedures. Use
  48. <code class="literal">INOUT</code> instead.)
  49. </p></dd><dt><span class="term"><em class="replaceable"><code>argname</code></em></span></dt><dd><p>
  50. The name of an argument.
  51. </p></dd><dt><span class="term"><em class="replaceable"><code>argtype</code></em></span></dt><dd><p>
  52. The data type(s) of the procedure's arguments (optionally
  53. schema-qualified), if any. The argument types can be base, composite,
  54. or domain types, or can reference the type of a table column.
  55. </p><p>
  56. Depending on the implementation language it might also be allowed
  57. to specify <span class="quote">“<span class="quote">pseudo-types</span>”</span> such as <code class="type">cstring</code>.
  58. Pseudo-types indicate that the actual argument type is either
  59. incompletely specified, or outside the set of ordinary SQL data types.
  60. </p><p>
  61. The type of a column is referenced by writing
  62. <code class="literal"><em class="replaceable"><code>table_name</code></em>.<em class="replaceable"><code>column_name</code></em>%TYPE</code>.
  63. Using this feature can sometimes help make a procedure independent of
  64. changes to the definition of a table.
  65. </p></dd><dt><span class="term"><em class="replaceable"><code>default_expr</code></em></span></dt><dd><p>
  66. An expression to be used as default value if the parameter is
  67. not specified. The expression has to be coercible to the
  68. argument type of the parameter.
  69. All input parameters following a
  70. parameter with a default value must have default values as well.
  71. </p></dd><dt><span class="term"><em class="replaceable"><code>lang_name</code></em></span></dt><dd><p>
  72. The name of the language that the procedure is implemented in.
  73. It can be <code class="literal">sql</code>, <code class="literal">c</code>,
  74. <code class="literal">internal</code>, or the name of a user-defined
  75. procedural language, e.g. <code class="literal">plpgsql</code>. Enclosing the
  76. name in single quotes is deprecated and requires matching case.
  77. </p></dd><dt><span class="term"><code class="literal">TRANSFORM { FOR TYPE <em class="replaceable"><code>type_name</code></em> } [, ... ] }</code></span></dt><dd><p>
  78. Lists which transforms a call to the procedure should apply. Transforms
  79. convert between SQL types and language-specific data types;
  80. see <a class="xref" href="sql-createtransform.html" title="CREATE TRANSFORM"><span class="refentrytitle">CREATE TRANSFORM</span></a>. Procedural language
  81. implementations usually have hardcoded knowledge of the built-in types,
  82. so those don't need to be listed here. If a procedural language
  83. implementation does not know how to handle a type and no transform is
  84. supplied, it will fall back to a default behavior for converting data
  85. types, but this depends on the implementation.
  86. </p></dd><dt><span class="term"><code class="literal">[<span class="optional">EXTERNAL</span>] SECURITY INVOKER</code><br /></span><span class="term"><code class="literal">[<span class="optional">EXTERNAL</span>] SECURITY DEFINER</code></span></dt><dd><p><code class="literal">SECURITY INVOKER</code> indicates that the procedure
  87. is to be executed with the privileges of the user that calls it.
  88. That is the default. <code class="literal">SECURITY DEFINER</code>
  89. specifies that the procedure is to be executed with the
  90. privileges of the user that owns it.
  91. </p><p>
  92. The key word <code class="literal">EXTERNAL</code> is allowed for SQL
  93. conformance, but it is optional since, unlike in SQL, this feature
  94. applies to all procedures not only external ones.
  95. </p><p>
  96. A <code class="literal">SECURITY DEFINER</code> procedure cannot execute
  97. transaction control statements (for example, <code class="command">COMMIT</code>
  98. and <code class="command">ROLLBACK</code>, depending on the language).
  99. </p></dd><dt><span class="term"><em class="replaceable"><code>configuration_parameter</code></em><br /></span><span class="term"><em class="replaceable"><code>value</code></em></span></dt><dd><p>
  100. The <code class="literal">SET</code> clause causes the specified configuration
  101. parameter to be set to the specified value when the procedure is
  102. entered, and then restored to its prior value when the procedure exits.
  103. <code class="literal">SET FROM CURRENT</code> saves the value of the parameter that
  104. is current when <code class="command">CREATE PROCEDURE</code> is executed as the value
  105. to be applied when the procedure is entered.
  106. </p><p>
  107. If a <code class="literal">SET</code> clause is attached to a procedure, then
  108. the effects of a <code class="command">SET LOCAL</code> command executed inside the
  109. procedure for the same variable are restricted to the procedure: the
  110. configuration parameter's prior value is still restored at procedure exit.
  111. However, an ordinary
  112. <code class="command">SET</code> command (without <code class="literal">LOCAL</code>) overrides the
  113. <code class="literal">SET</code> clause, much as it would do for a previous <code class="command">SET
  114. LOCAL</code> command: the effects of such a command will persist after
  115. procedure exit, unless the current transaction is rolled back.
  116. </p><p>
  117. If a <code class="literal">SET</code> clause is attached to a procedure, then
  118. that procedure cannot execute transaction control statements (for
  119. example, <code class="command">COMMIT</code> and <code class="command">ROLLBACK</code>,
  120. depending on the language).
  121. </p><p>
  122. See <a class="xref" href="sql-set.html" title="SET"><span class="refentrytitle">SET</span></a> and
  123. <a class="xref" href="runtime-config.html" title="Chapter 19. Server Configuration">Chapter 19</a>
  124. for more information about allowed parameter names and values.
  125. </p></dd><dt><span class="term"><em class="replaceable"><code>definition</code></em></span></dt><dd><p>
  126. A string constant defining the procedure; the meaning depends on the
  127. language. It can be an internal procedure name, the path to an
  128. object file, an SQL command, or text in a procedural language.
  129. </p><p>
  130. It is often helpful to use dollar quoting (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 procedure definition
  131. string, rather than the normal single quote syntax. Without dollar
  132. quoting, any single quotes or backslashes in the procedure definition must
  133. be escaped by doubling them.
  134. </p></dd><dt><span class="term"><code class="literal"><em class="replaceable"><code>obj_file</code></em>, <em class="replaceable"><code>link_symbol</code></em></code></span></dt><dd><p>
  135. This form of the <code class="literal">AS</code> clause is used for
  136. dynamically loadable C language procedures when the procedure name
  137. in the C language source code is not the same as the name of
  138. the SQL procedure. The string <em class="replaceable"><code>obj_file</code></em> is the name of the shared
  139. library file containing the compiled C procedure, and is interpreted
  140. as for the <a class="xref" href="sql-load.html" title="LOAD"><span class="refentrytitle">LOAD</span></a> command. The string
  141. <em class="replaceable"><code>link_symbol</code></em> is the
  142. procedure's link symbol, that is, the name of the procedure in the C
  143. language source code. If the link symbol is omitted, it is assumed
  144. to be the same as the name of the SQL procedure being defined.
  145. </p><p>
  146. When repeated <code class="command">CREATE PROCEDURE</code> calls refer to
  147. the same object file, the file is only loaded once per session.
  148. To unload and
  149. reload the file (perhaps during development), start a new session.
  150. </p></dd></dl></div></div><div class="refsect1" id="SQL-CREATEPROCEDURE-NOTES"><h2>Notes</h2><p>
  151. See <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a> for more details on function
  152. creation that also apply to procedures.
  153. </p><p>
  154. Use <a class="xref" href="sql-call.html" title="CALL"><span class="refentrytitle">CALL</span></a> to execute a procedure.
  155. </p></div><div class="refsect1" id="SQL-CREATEPROCEDURE-EXAMPLES"><h2>Examples</h2><pre class="programlisting">
  156. CREATE PROCEDURE insert_data(a integer, b integer)
  157. LANGUAGE SQL
  158. AS $$
  159. INSERT INTO tbl VALUES (a);
  160. INSERT INTO tbl VALUES (b);
  161. $$;
  162. CALL insert_data(1, 2);
  163. </pre></div><div class="refsect1" id="SQL-CREATEPROCEDURE-COMPAT"><h2>Compatibility</h2><p>
  164. A <code class="command">CREATE PROCEDURE</code> command is defined in the SQL
  165. standard. The <span class="productname">PostgreSQL</span> version is similar but
  166. not fully compatible. For details see
  167. also <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a>.
  168. </p></div><div class="refsect1" id="id-1.9.3.76.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alterprocedure.html" title="ALTER PROCEDURE"><span class="refentrytitle">ALTER PROCEDURE</span></a>, <a class="xref" href="sql-dropprocedure.html" title="DROP PROCEDURE"><span class="refentrytitle">DROP PROCEDURE</span></a>, <a class="xref" href="sql-call.html" title="CALL"><span class="refentrytitle">CALL</span></a>, <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createpolicy.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createpublication.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE POLICY </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> CREATE PUBLICATION</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1