首先,您尝试使用+
运算符连接两个字符串,但是用于连接的 SQL 运算符是||
,有了这些信息,您可能会认为预期的结果是(将不起作用):
\set path '''c:\\server\\data\\'''
COPY paymentMethods (name,regexString) FROM :path || 'paymentMethods.csv' WITH (FORMAT csv, HEADER true);
COPY priceLevels (name) FROM :path || 'priceLevels.csv' WITH (FORMAT csv, HEADER false);
但!该COPY
命令需要路径的文字字符串,而不是表达式,因此您确实应该提供路径。请注意,它适用于SELECT
, INSERT
,UPDATE
等命令。
有了这些信息,您只能使用psql
变量,正如 Pavel 指出的那样,并将字符串连接到 apsql
的变量中。一个好的解决方案是使用psql
's:'var'
语法,将变量作为字符串插入到 SQL 表达式中:
\set path 'c:\\server\\data\\'
\set paymentMethodsPath :path 'paymentMethods.csv'
\set priceLevelsPath :path 'priceLevels.csv'
COPY paymentMethods (name,regexString) FROM :'paymentMethodsPath' WITH (FORMAT csv, HEADER true);
COPY priceLevels (name) FROM :'priceLevels' WITH (FORMAT csv, HEADER false);
这将生成(将发送到 PostgreSQL 的服务器):
COPY paymentMethods (name,regexString) FROM E'c:\\server\\data\\paymentMethods.csv' WITH (FORMAT csv, HEADER true);
COPY priceLevels (name) FROM E'c:\\server\\data\\priceLevels.csv' WITH (FORMAT csv, HEADER false);
此:'var'
语法不适用于所有psql
版本(我现在不记得引入了哪一个),但对于旧版本,您可以轻松使用美元引用:
\set path 'c:\\server\\data\\'
\set paymentMethodsPath :path 'paymentMethods.csv'
\set priceLevelsPath :path 'priceLevels.csv'
COPY paymentMethods (name,regexString) FROM $$:paymentMethodsPath$$ WITH (FORMAT csv, HEADER true);
COPY priceLevels (name) FROM $$:priceLevels$$ WITH (FORMAT csv, HEADER false);
或转义为单引号:
\set path 'c:\\server\\data\\'
\set paymentMethodsPath 'E''':path'paymentMethods.csv'''
\set priceLevelsPath 'E''':path'priceLevels.csv'''
COPY paymentMethods (name,regexString) FROM :paymentMethodsPath WITH (FORMAT csv, HEADER true);
COPY priceLevels (name) FROM :priceLevels WITH (FORMAT csv, HEADER false);
这就对了。