我的函数定义是这样的,它抛出了一个错误
CREATE OR REPLACE FUNCTION traffic.addcsv (
filename text
)
RETURNS void AS
$body$
BEGIN
DROP TABLE IF EXISTS traffic.tsdata;
create table if not EXISTS traffic.tsdata
(
ldir text,
dtime timestamp,
epoch integer,
ln_m integer,
freeflow real,
spdlimit integer,
cnt integer,
mean_spd real,
stddev real,
mn integer,
mx integer,
conf integer
);
COPY traffic.tsdata FROM '$1' DELIMITER ',' CSV HEADER;
CREATE TABLE IF NOT EXISTS traffic.psdata
(
l_id BIGINT,
direction char(1),
dtime timestamp,
day_number SMALLINT,
epoch smallint,
length_m integer,
freeflow smallint,
spdlimit smallint,
cnt integer,
mean_speed smallint,
stddev double precision ,
mn smallint,
mx SMALLINT,
confidence smallint
);
INSERT INTO traffic.psdata (
l_id,
direction,
dtime,
day_number,
epoch,
length_m,
freeflow,
spdlimit,
cnt,
mean_speed,
stddev,
mn,
mx,
confidence
)
SELECT
(replace ( (replace(ldir,'F','')) ,'T','' )::integer,
RIGHT(tsdata.ldir,1)::char(1) ,
dtime,
EXTRACT(ISODOW FROM traffic.tsdata.dtime)::smallint ,
epoch,
ln_m,
freeflow,
spdlimit,
cnt,
mean_spd,
stddev,
mn,
mx,
conf)
FROM traffic.tsdata;
END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
PARALLEL UNSAFE
COST 100;
错误:无法打开文件“$ 1”进行读取:没有这样的文件或目录提示:COPY FROM指示PostgreSQL服务器进程读取文件。您可能需要一个客户端设施,例如psql的\ copy。语境:SQL语句“COPY traffic.tsdata FROM'$ 1'DELIMITER','CSV HEADER”PL / pgSQL函数traffic.addcsv(text)第21行SQL语句
我不认为PL / pgSQL允许在COPY
语句中使用变量。
您可以使用动态SQL执行此操作:
EXECUTE format('COPY traffic.tsdata FROM %L DELIMITER '','' CSV HEADER', $1);