0
CREATE OR REPLACE RULE INSERT_part_name AS
   ON INSERT TO partition.part_name
   DO INSTEAD select crm.dblink_exec('crm_server'::text, 'insert into partition.part_name(customerid, partition_name, partition, createdate) values('||new.customerid||','||quote_literal(new.partition_name)||',false'::text ||','||quote_literal(new.createdate))::timestamp without time zone);
错误:“)”处或附近的语法错误
LINE 3: ...uote_literal(new.createdate))::timestamp without time zone);
                                                                ^
4

1 回答 1

0

不确定在规则中使用 dblink 的想法(未测试)。但是你有一个关闭)太多:

CREATE OR REPLACE RULE INSERT_part_name AS
ON INSERT TO partition.part_name
DO INSTEAD SELECT crm.dblink_exec('crm_server'::text,
   'insert into partition.part_name(customerid, partition_name, partition, createdate)
    values(' || new.customerid || ',' || quote_literal(new.partition_name) || ',false'
    || ',' || quote_literal(new.createdate::timestamp without time zone));

不过,我会使用format()更简洁的代码(需要 Postgres 9.1+):

CREATE OR REPLACE RULE INSERT_part_name AS
ON INSERT TO partition.part_name
DO INSTEAD SELECT crm.dblink_exec('crm_server'::text, format(
 'INSERT INTO partition.part_name(customerid, partition_name, partition, createdate)
  VALUES(%s, %L, false, %L)'
 ,NEW.customerid, NEW.partition_name, NEW.createdate::timestamp));
于 2013-10-09T16:16:07.230 回答