我已经为帐户表创建了XML格式文件,它使用以下方式创建:选择table_to_xml(' account ',true,FALSE,'');
-表结构为:创建表public.account ( account_id整数NULL,名称VARCHAR(1) NULL,类型VARCHAR(20),group_name VARCHAR(50),约束account_pkey主键(Account_id));
问题:如何使用PostgreSQL中的XML文件直接将数据加载到帐户表中?
发布于 2016-05-14 12:02:10
由于xml的转换,我不得不使用varchar(2)。
我使用了select into (创建public.account)
select account_id::text::int, account_name::varchar(2),
account_type::varchar(20) , account_group::varchar(50) INTO
public.account from(
WITH x AS ( SELECT
'<accounts>
<account>
<account_id>1</account_id>
<account_name> A </account_name>
<account_type> off shore</account_type>
<account_group> slush fund </account_group>
</account>
<account>
<account_id>3</account_id>
<account_name> C </account_name>
<account_type> off shore</account_type>
<account_group> slush fund </account_group>
</account>
</accounts> '::xml AS t)
SELECT unnest(xpath('/accounts/account/account_id/text()', t)) 
AS account_id,
   unnest(xpath('/accounts/account/account_name/text()', t)) 
AS account_name,
   unnest(xpath('/accounts/account/account_type/text()', t)) 
AS account_type,
   unnest(xpath('/accounts/account/account_group/text()', t)) 
AS account_group
FROM   x) as accounts如果您对读取xml文件感兴趣,那么这可能是有用的。
参考文献stackexchange sql to read xml from file into postgresql
我希望这能帮到你
https://stackoverflow.com/questions/37183542
复制相似问题