我的函数定义是这种方式,并且抛出错误
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语句“从'$ 1'DELIMITER','CSV HEADER复制COPY traffic.tsdata” PL / pgSQL函数traffic.addcsv(text)在SQL语句的第21行
答案 0 :(得分:0)
我认为PL / pgSQL不允许在COPY
语句中使用变量。
您可以使用动态SQL来做到这一点:
EXECUTE format('COPY traffic.tsdata FROM %L DELIMITER '','' CSV HEADER', $1);