Postgresql 中文操作指南

dblink_exec — 在远程数据库中执行命令

dblink_exec — executes a command in a remote database

Synopsis

dblink_exec(text connname, text sql [, bool fail_on_error]) returns text
dblink_exec(text connstr, text sql [, bool fail_on_error]) returns text
dblink_exec(text sql [, bool fail_on_error]) returns text

Description

dblink_exec 在远程数据库中执行命令(即任何不返回行的 SQL 语句)。

dblink_exec executes a command (that is, any SQL statement that doesn’t return rows) in a remote database.

当给定两个 text 参数时,第一个参数首先作为持久连接的名称进行查找;如果找到,该命令便在这种连接上执行。如果未找到,第一个参数将被视为 dblink_connect 的连接信息字符串,并且只是在此命令的持续时间内建立指示的连接。

When two text arguments are given, the first one is first looked up as a persistent connection’s name; if found, the command is executed on that connection. If not found, the first argument is treated as a connection info string as for dblink_connect, and the indicated connection is made just for the duration of this command.

Arguments

  • connname

    • Name of the connection to use; omit this parameter to use the unnamed connection.

  • connstr

    • A connection info string, as previously described for dblink_connect.

  • sql

    • The SQL command that you wish to execute in the remote database, for example insert into foo values(0, 'a', '{"a0","b0","c0"}').

  • fail_on_error

    • If true (the default when omitted) then an error thrown on the remote side of the connection causes an error to also be thrown locally. If false, the remote error is locally reported as a NOTICE, and the function’s return value is set to ERROR.

Return Value

返回状态,可能是命令的状态字符串或 ERROR

Returns status, either the command’s status string or ERROR.

Examples

SELECT dblink_connect('dbname=dblink_test_standby');
 dblink_connect
----------------
 OK
(1 row)

SELECT dblink_exec('insert into foo values(21, ''z'', ''{"a0","b0","c0"}'');');
   dblink_exec
-----------------
 INSERT 943366 1
(1 row)

SELECT dblink_connect('myconn', 'dbname=regression');
 dblink_connect
----------------
 OK
(1 row)

SELECT dblink_exec('myconn', 'insert into foo values(21, ''z'', ''{"a0","b0","c0"}'');');
   dblink_exec
------------------
 INSERT 6432584 1
(1 row)

SELECT dblink_exec('myconn', 'insert into pg_class values (''foo'')',false);
NOTICE:  sql error
DETAIL:  ERROR:  null value in column "relnamespace" violates not-null constraint

 dblink_exec
-------------
 ERROR
(1 row)