gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

143 lines
8.5KB

  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>dblink</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="contrib-dblink-disconnect.html" title="dblink_disconnect" /><link rel="next" href="contrib-dblink-exec.html" title="dblink_exec" /></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">dblink</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="contrib-dblink-disconnect.html" title="dblink_disconnect">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="dblink.html" title="F.10. dblink">Up</a></td><th width="60%" align="center">F.10. dblink</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="contrib-dblink-exec.html" title="dblink_exec">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="CONTRIB-DBLINK-FUNCTION"><div class="titlepage"></div><a id="id-1.11.7.19.8.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">dblink</span></h2><p>dblink — executes a query in a remote database</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. dblink(text connname, text sql [, bool fail_on_error]) returns setof record
  4. dblink(text connstr, text sql [, bool fail_on_error]) returns setof record
  5. dblink(text sql [, bool fail_on_error]) returns setof record
  6. </pre></div><div class="refsect1" id="id-1.11.7.19.8.5"><h2>Description</h2><p>
  7. <code class="function">dblink</code> executes a query (usually a <code class="command">SELECT</code>,
  8. but it can be any SQL statement that returns rows) in a remote database.
  9. </p><p>
  10. When two <code class="type">text</code> arguments are given, the first one is first
  11. looked up as a persistent connection's name; if found, the command
  12. is executed on that connection. If not found, the first argument
  13. is treated as a connection info string as for <code class="function">dblink_connect</code>,
  14. and the indicated connection is made just for the duration of this command.
  15. </p></div><div class="refsect1" id="id-1.11.7.19.8.6"><h2>Arguments</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="parameter"><code>connname</code></em></span></dt><dd><p>
  16. Name of the connection to use; omit this parameter to use the
  17. unnamed connection.
  18. </p></dd><dt><span class="term"><em class="parameter"><code>connstr</code></em></span></dt><dd><p>
  19. A connection info string, as previously described for
  20. <code class="function">dblink_connect</code>.
  21. </p></dd><dt><span class="term"><em class="parameter"><code>sql</code></em></span></dt><dd><p>
  22. The SQL query that you wish to execute in the remote database,
  23. for example <code class="literal">select * from foo</code>.
  24. </p></dd><dt><span class="term"><em class="parameter"><code>fail_on_error</code></em></span></dt><dd><p>
  25. If true (the default when omitted) then an error thrown on the
  26. remote side of the connection causes an error to also be thrown
  27. locally. If false, the remote error is locally reported as a NOTICE,
  28. and the function returns no rows.
  29. </p></dd></dl></div></div><div class="refsect1" id="id-1.11.7.19.8.7"><h2>Return Value</h2><p>
  30. The function returns the row(s) produced by the query. Since
  31. <code class="function">dblink</code> can be used with any query, it is declared
  32. to return <code class="type">record</code>, rather than specifying any particular
  33. set of columns. This means that you must specify the expected
  34. set of columns in the calling query — otherwise
  35. <span class="productname">PostgreSQL</span> would not know what to expect.
  36. Here is an example:
  37. </p><pre class="programlisting">
  38. SELECT *
  39. FROM dblink('dbname=mydb options=-csearch_path=',
  40. 'select proname, prosrc from pg_proc')
  41. AS t1(proname name, prosrc text)
  42. WHERE proname LIKE 'bytea%';
  43. </pre><p>
  44. The <span class="quote">“<span class="quote">alias</span>”</span> part of the <code class="literal">FROM</code> clause must
  45. specify the column names and types that the function will return.
  46. (Specifying column names in an alias is actually standard SQL
  47. syntax, but specifying column types is a <span class="productname">PostgreSQL</span>
  48. extension.) This allows the system to understand what
  49. <code class="literal">*</code> should expand to, and what <code class="structname">proname</code>
  50. in the <code class="literal">WHERE</code> clause refers to, in advance of trying
  51. to execute the function. At run time, an error will be thrown
  52. if the actual query result from the remote database does not
  53. have the same number of columns shown in the <code class="literal">FROM</code> clause.
  54. The column names need not match, however, and <code class="function">dblink</code>
  55. does not insist on exact type matches either. It will succeed
  56. so long as the returned data strings are valid input for the
  57. column type declared in the <code class="literal">FROM</code> clause.
  58. </p></div><div class="refsect1" id="id-1.11.7.19.8.8"><h2>Notes</h2><p>
  59. A convenient way to use <code class="function">dblink</code> with predetermined
  60. queries is to create a view.
  61. This allows the column type information to be buried in the view,
  62. instead of having to spell it out in every query. For example,
  63. </p><pre class="programlisting">
  64. CREATE VIEW myremote_pg_proc AS
  65. SELECT *
  66. FROM dblink('dbname=postgres options=-csearch_path=',
  67. 'select proname, prosrc from pg_proc')
  68. AS t1(proname name, prosrc text);
  69. SELECT * FROM myremote_pg_proc WHERE proname LIKE 'bytea%';
  70. </pre></div><div class="refsect1" id="id-1.11.7.19.8.9"><h2>Examples</h2><pre class="screen">
  71. SELECT * FROM dblink('dbname=postgres options=-csearch_path=',
  72. 'select proname, prosrc from pg_proc')
  73. AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
  74. proname | prosrc
  75. ------------+------------
  76. byteacat | byteacat
  77. byteaeq | byteaeq
  78. bytealt | bytealt
  79. byteale | byteale
  80. byteagt | byteagt
  81. byteage | byteage
  82. byteane | byteane
  83. byteacmp | byteacmp
  84. bytealike | bytealike
  85. byteanlike | byteanlike
  86. byteain | byteain
  87. byteaout | byteaout
  88. (12 rows)
  89. SELECT dblink_connect('dbname=postgres options=-csearch_path=');
  90. dblink_connect
  91. ----------------
  92. OK
  93. (1 row)
  94. SELECT * FROM dblink('select proname, prosrc from pg_proc')
  95. AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
  96. proname | prosrc
  97. ------------+------------
  98. byteacat | byteacat
  99. byteaeq | byteaeq
  100. bytealt | bytealt
  101. byteale | byteale
  102. byteagt | byteagt
  103. byteage | byteage
  104. byteane | byteane
  105. byteacmp | byteacmp
  106. bytealike | bytealike
  107. byteanlike | byteanlike
  108. byteain | byteain
  109. byteaout | byteaout
  110. (12 rows)
  111. SELECT dblink_connect('myconn', 'dbname=regression options=-csearch_path=');
  112. dblink_connect
  113. ----------------
  114. OK
  115. (1 row)
  116. SELECT * FROM dblink('myconn', 'select proname, prosrc from pg_proc')
  117. AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
  118. proname | prosrc
  119. ------------+------------
  120. bytearecv | bytearecv
  121. byteasend | byteasend
  122. byteale | byteale
  123. byteagt | byteagt
  124. byteage | byteage
  125. byteane | byteane
  126. byteacmp | byteacmp
  127. bytealike | bytealike
  128. byteanlike | byteanlike
  129. byteacat | byteacat
  130. byteaeq | byteaeq
  131. bytealt | bytealt
  132. byteain | byteain
  133. byteaout | byteaout
  134. (14 rows)
  135. </pre></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="contrib-dblink-disconnect.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="dblink.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="contrib-dblink-exec.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">dblink_disconnect </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> dblink_exec</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1