0

我正在尝试简化以下最终陈述:

DROP TABLE asdf;

CREATE TABLE asdf (LIKE calibrations);

INSERT INTO asdf (rid, pid, status, cal_date, project_name, username,
  sensor_type, serial_number, var_name, dsm_name, cal_type, channel,
  gainbplr, ads_file_name, set_times, set_points, averages, stddevs, cal,
  temperature, comment)
  SELECT * FROM dblink(
    'host=remoteserv user=auser password=hello dbname=cals',
    'SELECT * FROM calibrations WHERE username=\'JAM\'') AS
      t(rid character varying(20), pid character varying(20),
        status character(3), cal_date timestamp,
        project_name character varying(32), username character varying(32),
        sensor_type character varying(20), sn character varying(20),
        var_name character varying(20), dsm_name character varying(16),
        cal_type character varying(16), channel character(1),
        gainbplr character(2), ads_file_name character varying(40),
        set_times timestamp[], set_points double precision[],
        averages double precision[], stddevs double precision[],
        cal double precision[], temperature double precision,
        comment character varying(256));

我能够像这样简化最后一条语句:

INSERT INTO asdf 
  SELECT * FROM dblink(
    'host=remoteserv user=auser password=hello dbname=cals',
    'SELECT * FROM calibrations WHERE username=\'JAM\'') AS
      t(rid character varying(20), pid character varying(20),
        status character(3), cal_date timestamp,
        project_name character varying(32), username character varying(32),
        sensor_type character varying(20), sn character varying(20),
        var_name character varying(20), dsm_name character varying(16),
        cal_type character varying(16), channel character(1),
        gainbplr character(2), ads_file_name character varying(40),
        set_times timestamp[], set_points double precision[],
        averages double precision[], stddevs double precision[],
        cal double precision[], temperature double precision,
        comment character varying(256));

是否可以将最后一条语句写成这样?

INSERT INTO asdf 
  SELECT * FROM dblink(
    'host=remoteserv user=auser password=hello dbname=cals',
    'SELECT * FROM calibrations WHERE username=\'JAM\'') AS
      t(LIKE calibrations);

- 约翰

4

1 回答 1

0

不,dblink() 函数返回setof record并且数据库需要知道列列表。这在 dblink 文档中提到:

该函数返回查询产生的行。由于 dblink 可用于任何查询,因此声明它返回记录,而不是指定任何特定的列集。这意味着您必须在调用查询中指定预期的列集——否则 PostgreSQL 将不知道会发生什么。

FROM语句子句的定义SELECT如下:

where from_item can be one of:

    [ ONLY ] table_name [ * ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
    ( select ) [ AS ] alias [ ( column_alias [, ...] ) ]
    with_query_name [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
    function_name ( [ argument [, ...] ] ) [ AS ] alias [ ( column_alias [, ...] | column_definition [, ...] ) ]
    function_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] )
    from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column [, ...] ) ]
于 2012-11-20T06:37:50.960 回答