将XML文件中的数据插入现有的SQL Server表中

时间:2016-12-05 08:31:26

标签: sql-server-2014 sqlxml

我需要将XML文件中的数据插入到现有的SQL SERVER表中。我已将以下代码修改为INSERT INTO,但它无效。我不想在此声明中删除或创建新表

DECLARE @x xml
SELECT @x=P
FROM OPENROWSET (BULK 'D:\Course Instance Marketing Data From Drupal\instance_marketing.xml', SINGLE_BLOB ) AS COURSE_INSTANCE(P)

DECLARE @hdoc int

EXEC sp_xml_preparedocument @hdoc OUTPUT, @x


SELECT *
INTO RCMI_MARKETING_SYNC
FROM OPENXML(@hdoc,'/response/item', 2)
 WITH(
 CourseInstanceKey int '@key',
idRCMI_MARKETING_SYNC int '@key' ,
title varchar(3000),
course_code varchar(3000),
market_area varchar(3000),
ssa varchar(3000),
school_owning varchar(3000),
overview varchar(3000), 
entry_requirements varchar(3000),
teaching_methods varchar(3000),
modules_and_assessment varchar(3000),
career_options_progres varchar(3000),
equipment_needed_costs varchar(3000),
work_placement_field_trips varchar(3000)
)   

  EXEC sp_xml_removedocument @hdoc

错误

Msg 2714, Level 16, State 6, Line 10
There is already an object named 'RCMI_MARKETING_SYNC' in the database.

我不想删除现有表格

1 个答案:

答案 0 :(得分:1)

找到答案

DECLARE @x xml
SELECT @x=P
FROM OPENROWSET (BULK 'D:\Course Instance Marketing Data From Drupal\instance_marketing.xml', SINGLE_BLOB ) AS COURSE_INSTANCE(P)

DECLARE @hdoc int

EXEC sp_xml_preparedocument @hdoc OUTPUT, @x

INSERT INTO RCMI_MARKETING_SYNC
SELECT *
FROM OPENXML(@hdoc,'/response/item', 2)
WITH(
CourseInstanceKey int '@key',
idRCMI_MARKETING_SYNC int '@key' ,
title varchar(3000),
course_code varchar(3000),
market_area varchar(3000),
ssa varchar(3000),
school_owning varchar(3000),
overview varchar(3000), 
entry_requirements varchar(3000),
teaching_methods varchar(3000),
modules_and_assessment varchar(3000),
career_options_progres varchar(3000),
equipment_needed_costs varchar(3000),
work_placement_field_trips varchar(3000)
)   

  EXEC sp_xml_removedocument @hdoc