1

我有一张下表

CREATE TABLE usr_addr_part_15
(
  company character varying(70),
  add1 character varying(50),
  add2 character varying(50),
  add3 character varying(50),
  phoneoff character varying(30)
)

样本数据如下

INSERT INTO usr_addr_part_15
VALUES (
    'A Compnay'
    ,'35/908A4, KINGS ARCADE, NORTH ATLANTA ROAD'
    ,'FL'
    ,'WS-600 025'
    ,'011-200455/255477'
    )
    ,(
    'B Company'
    ,'35/465R, QUEENS ARCADE, WEST ATLANTA ROAD'
    ,'FL'
    ,'WT-601 085'
    ,'0225-2455215/21470277'
    )

所以我需要COPY将内容放入如下给定格式usr_addr_part_15文本文件中:

COMPANY : A Compnay                               
ADD1 : 35/908A4,KINGS ARCADE,NORTH ATLANTA ROAD 
ADD2 : FL                                         
ADD3 : WS-600 025                                 
PHONE(Off.) : 011-200455/255477                   
--- End Of Company 1 ---                          
COMPANY : B Company                               
ADD1 : 35/465R,QUEENS ARCADE,WEST ATLANTA ROAD  
ADD2 : FL                                         
ADD3 : WT-601 085                                 
PHONE(Off.) : 0225-2455215/21470277               
--- End Of Company 2 ---  

在 Windows XP 上,pgAdmin III。

4

1 回答 1

0

如果我理解正确,您可以这样做:

SELECT unnest(format('{COMPANY : %s,ADD1 : %s,ADD2 : %s,ADD3 : %s,PHONE(Off.) : %s,--- End Of Company %s ---}', company, replace(add1, ',', ';'), replace(add2, ',', ';'),  replace(add3, ',', ';'),phoneoff, row_number() OVER ())::TEXT [])
FROM usr_addr_part_15

并通过查询复制

copy(
SELECT unnest(format('{COMPANY : %s,ADD1 : %s,ADD2 : %s,ADD3 : %s,PHONE(Off.) : %s,--- End Of Company %s ---}', company, replace(add1, ',', ';'), replace(add2, ',', ';'),  replace(add3, ',', ';'),phoneoff, row_number() OVER ())::TEXT [])
FROM usr_addr_part_15
)
to 'D:\test.txt' -- the file name that you want save the data
于 2015-04-25T07:14:20.030 回答