我正在尝试简化以下最终声明:
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);
- 约翰
答案 0 :(得分: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 [, ...] ) ]