]> begriffs open source - ai-pg/blob - full-docs/txt/contrib-dblink-build-sql-insert.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / contrib-dblink-build-sql-insert.txt
1
2 dblink_build_sql_insert
3
4    dblink_build_sql_insert — builds an INSERT statement using a local
5    tuple, replacing the primary key field values with alternative supplied
6    values
7
8 Synopsis
9
10 dblink_build_sql_insert(text relname,
11                         int2vector primary_key_attnums,
12                         integer num_primary_key_atts,
13                         text[] src_pk_att_vals_array,
14                         text[] tgt_pk_att_vals_array) returns text
15
16 Description
17
18    dblink_build_sql_insert can be useful in doing selective replication of
19    a local table to a remote database. It selects a row from the local
20    table based on primary key, and then builds an SQL INSERT command that
21    will duplicate that row, but with the primary key values replaced by
22    the values in the last argument. (To make an exact copy of the row,
23    just specify the same values for the last two arguments.)
24
25 Arguments
26
27    relname
28           Name of a local relation, for example foo or myschema.mytab.
29           Include double quotes if the name is mixed-case or contains
30           special characters, for example "FooBar"; without quotes, the
31           string will be folded to lower case.
32
33    primary_key_attnums
34           Attribute numbers (1-based) of the primary key fields, for
35           example 1 2.
36
37    num_primary_key_atts
38           The number of primary key fields.
39
40    src_pk_att_vals_array
41           Values of the primary key fields to be used to look up the local
42           tuple. Each field is represented in text form. An error is
43           thrown if there is no local row with these primary key values.
44
45    tgt_pk_att_vals_array
46           Values of the primary key fields to be placed in the resulting
47           INSERT command. Each field is represented in text form.
48
49 Return Value
50
51    Returns the requested SQL statement as text.
52
53 Notes
54
55    As of PostgreSQL 9.0, the attribute numbers in primary_key_attnums are
56    interpreted as logical column numbers, corresponding to the column's
57    position in SELECT * FROM relname. Previous versions interpreted the
58    numbers as physical column positions. There is a difference if any
59    column(s) to the left of the indicated column have been dropped during
60    the lifetime of the table.
61
62 Examples
63
64 SELECT dblink_build_sql_insert('foo', '1 2', 2, '{"1", "a"}', '{"1", "b''a"}');
65              dblink_build_sql_insert
66 --------------------------------------------------
67  INSERT INTO foo(f1,f2,f3) VALUES('1','b''a','1')
68 (1 row)