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

135 行
11KB

  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>VALUES</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-vacuum.html" title="VACUUM" /><link rel="next" href="reference-client.html" title="PostgreSQL Client Applications" /></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">VALUES</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-vacuum.html" title="VACUUM">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="reference-client.html" title="PostgreSQL Client Applications">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-VALUES"><div class="titlepage"></div><a id="id-1.9.3.184.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">VALUES</span></h2><p>VALUES — compute a set of rows</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. VALUES ( <em class="replaceable"><code>expression</code></em> [, ...] ) [, ...]
  4. [ ORDER BY <em class="replaceable"><code>sort_expression</code></em> [ ASC | DESC | USING <em class="replaceable"><code>operator</code></em> ] [, ...] ]
  5. [ LIMIT { <em class="replaceable"><code>count</code></em> | ALL } ]
  6. [ OFFSET <em class="replaceable"><code>start</code></em> [ ROW | ROWS ] ]
  7. [ FETCH { FIRST | NEXT } [ <em class="replaceable"><code>count</code></em> ] { ROW | ROWS } ONLY ]
  8. </pre></div><div class="refsect1" id="id-1.9.3.184.5"><h2>Description</h2><p>
  9. <code class="command">VALUES</code> computes a row value or set of row values
  10. specified by value expressions. It is most commonly used to generate
  11. a <span class="quote">“<span class="quote">constant table</span>”</span> within a larger command, but it can be
  12. used on its own.
  13. </p><p>
  14. When more than one row is specified, all the rows must have the same
  15. number of elements. The data types of the resulting table's columns are
  16. determined by combining the explicit or inferred types of the expressions
  17. appearing in that column, using the same rules as for <code class="literal">UNION</code>
  18. (see <a class="xref" href="typeconv-union-case.html" title="10.5. UNION, CASE, and Related Constructs">Section 10.5</a>).
  19. </p><p>
  20. Within larger commands, <code class="command">VALUES</code> is syntactically allowed
  21. anywhere that <code class="command">SELECT</code> is. Because it is treated like a
  22. <code class="command">SELECT</code> by the grammar, it is possible to use
  23. the <code class="literal">ORDER BY</code>, <code class="literal">LIMIT</code> (or
  24. equivalently <code class="literal">FETCH FIRST</code>),
  25. and <code class="literal">OFFSET</code> clauses with a
  26. <code class="command">VALUES</code> command.
  27. </p></div><div class="refsect1" id="id-1.9.3.184.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>expression</code></em></span></dt><dd><p>
  28. A constant or expression to compute and insert at the indicated place
  29. in the resulting table (set of rows). In a <code class="command">VALUES</code> list
  30. appearing at the top level of an <code class="command">INSERT</code>, an
  31. <em class="replaceable"><code>expression</code></em> can be replaced
  32. by <code class="literal">DEFAULT</code> to indicate that the destination column's
  33. default value should be inserted. <code class="literal">DEFAULT</code> cannot
  34. be used when <code class="command">VALUES</code> appears in other contexts.
  35. </p></dd><dt><span class="term"><em class="replaceable"><code>sort_expression</code></em></span></dt><dd><p>
  36. An expression or integer constant indicating how to sort the result
  37. rows. This expression can refer to the columns of the
  38. <code class="command">VALUES</code> result as <code class="literal">column1</code>, <code class="literal">column2</code>,
  39. etc. For more details see
  40. <a class="xref" href="sql-select.html#SQL-ORDERBY" title="ORDER BY Clause"><code class="literal">ORDER BY</code> Clause</a>.
  41. </p></dd><dt><span class="term"><em class="replaceable"><code>operator</code></em></span></dt><dd><p>
  42. A sorting operator. For details see
  43. <a class="xref" href="sql-select.html#SQL-ORDERBY" title="ORDER BY Clause"><code class="literal">ORDER BY</code> Clause</a>.
  44. </p></dd><dt><span class="term"><em class="replaceable"><code>count</code></em></span></dt><dd><p>
  45. The maximum number of rows to return. For details see
  46. <a class="xref" href="sql-select.html#SQL-LIMIT" title="LIMIT Clause"><code class="literal">LIMIT</code> Clause</a>.
  47. </p></dd><dt><span class="term"><em class="replaceable"><code>start</code></em></span></dt><dd><p>
  48. The number of rows to skip before starting to return rows.
  49. For details see
  50. <a class="xref" href="sql-select.html#SQL-LIMIT" title="LIMIT Clause"><code class="literal">LIMIT</code> Clause</a>.
  51. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.184.7"><h2>Notes</h2><p>
  52. <code class="command">VALUES</code> lists with very large numbers of rows should be avoided,
  53. as you might encounter out-of-memory failures or poor performance.
  54. <code class="command">VALUES</code> appearing within <code class="command">INSERT</code> is a special case
  55. (because the desired column types are known from the <code class="command">INSERT</code>'s
  56. target table, and need not be inferred by scanning the <code class="command">VALUES</code>
  57. list), so it can handle larger lists than are practical in other contexts.
  58. </p></div><div class="refsect1" id="id-1.9.3.184.8"><h2>Examples</h2><p>
  59. A bare <code class="command">VALUES</code> command:
  60. </p><pre class="programlisting">
  61. VALUES (1, 'one'), (2, 'two'), (3, 'three');
  62. </pre><p>
  63. This will return a table of two columns and three rows. It's effectively
  64. equivalent to:
  65. </p><pre class="programlisting">
  66. SELECT 1 AS column1, 'one' AS column2
  67. UNION ALL
  68. SELECT 2, 'two'
  69. UNION ALL
  70. SELECT 3, 'three';
  71. </pre><p>
  72. </p><p>
  73. More usually, <code class="command">VALUES</code> is used within a larger SQL command.
  74. The most common use is in <code class="command">INSERT</code>:
  75. </p><pre class="programlisting">
  76. INSERT INTO films (code, title, did, date_prod, kind)
  77. VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');
  78. </pre><p>
  79. </p><p>
  80. In the context of <code class="command">INSERT</code>, entries of a <code class="command">VALUES</code> list
  81. can be <code class="literal">DEFAULT</code> to indicate that the column default
  82. should be used here instead of specifying a value:
  83. </p><pre class="programlisting">
  84. INSERT INTO films VALUES
  85. ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes'),
  86. ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama', DEFAULT);
  87. </pre><p>
  88. </p><p>
  89. <code class="command">VALUES</code> can also be used where a sub-<code class="command">SELECT</code> might
  90. be written, for example in a <code class="literal">FROM</code> clause:
  91. </p><pre class="programlisting">
  92. SELECT f.*
  93. FROM films f, (VALUES('MGM', 'Horror'), ('UA', 'Sci-Fi')) AS t (studio, kind)
  94. WHERE f.studio = t.studio AND f.kind = t.kind;
  95. UPDATE employees SET salary = salary * v.increase
  96. FROM (VALUES(1, 200000, 1.2), (2, 400000, 1.4)) AS v (depno, target, increase)
  97. WHERE employees.depno = v.depno AND employees.sales &gt;= v.target;
  98. </pre><p>
  99. Note that an <code class="literal">AS</code> clause is required when <code class="command">VALUES</code>
  100. is used in a <code class="literal">FROM</code> clause, just as is true for
  101. <code class="command">SELECT</code>. It is not required that the <code class="literal">AS</code> clause
  102. specify names for all the columns, but it's good practice to do so.
  103. (The default column names for <code class="command">VALUES</code> are <code class="literal">column1</code>,
  104. <code class="literal">column2</code>, etc in <span class="productname">PostgreSQL</span>, but
  105. these names might be different in other database systems.)
  106. </p><p>
  107. When <code class="command">VALUES</code> is used in <code class="command">INSERT</code>, the values are all
  108. automatically coerced to the data type of the corresponding destination
  109. column. When it's used in other contexts, it might be necessary to specify
  110. the correct data type. If the entries are all quoted literal constants,
  111. coercing the first is sufficient to determine the assumed type for all:
  112. </p><pre class="programlisting">
  113. SELECT * FROM machines
  114. WHERE ip_address IN (VALUES('192.168.0.1'::inet), ('192.168.0.10'), ('192.168.1.43'));
  115. </pre><div class="tip"><h3 class="title">Tip</h3><p>
  116. For simple <code class="literal">IN</code> tests, it's better to rely on the
  117. <a class="link" href="functions-comparisons.html#FUNCTIONS-COMPARISONS-IN-SCALAR" title="9.23.1. IN">list-of-scalars</a>
  118. form of <code class="literal">IN</code> than to write a <code class="command">VALUES</code>
  119. query as shown above. The list of scalars method requires less writing
  120. and is often more efficient.
  121. </p></div></div><div class="refsect1" id="id-1.9.3.184.9"><h2>Compatibility</h2><p><code class="command">VALUES</code> conforms to the SQL standard.
  122. <code class="literal">LIMIT</code> and <code class="literal">OFFSET</code> are
  123. <span class="productname">PostgreSQL</span> extensions; see also
  124. under <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</span></a>.
  125. </p></div><div class="refsect1" id="id-1.9.3.184.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-insert.html" title="INSERT"><span class="refentrytitle">INSERT</span></a>, <a class="xref" href="sql-select.html" title="SELECT"><span class="refentrytitle">SELECT</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-vacuum.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="reference-client.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">VACUUM </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> PostgreSQL Client Applications</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1