简体   繁体   中英

How to add variable into path string in copy command PSQL

I'm using psql to run a .sql file to copy data from csv files into Postgres, but the folder contains csv files is changed everyday, so I want to add a variable into path code

This is my code

delete from table1;
delete from table2;
delete from table3;
\copy table1 FROM 'D:\tamlam\' + convert(date,getdate()) + '\file1.csv' DELIMITER ',' CSV HEADER QUOTE '"' NULL '';

Use the PROGRAM option.

See this documentation how to get date in a desired format in Windows batch.

Put that in a script which does TYPE %filenamevariable% at the end. Let's say the script is named data_gen . You may then do

\COPY table1 FROM PROGRAM '\scriptpath\data_gen'

For Unix users, it is simpler, date command with options can generate date in a format and set a variable. then simply do cat $filenamevariable at the end of program

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM