将数据从xml列插入临时表

时间:2014-01-01 06:59:48

标签: sql sql-server xml

我有一个看起来像

的xml列
SET @XMLData = '<ArrayOfEntityNested xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
                                     xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto.Bijak">
                   <EntityNested>
                       <Id xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto">1</Id>
                       <Date xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto.VirginBijak">0001-01-01T00:00:00</Date>
                       <Description xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto.VirginBijak">deesc</Description>
                       <Number xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto.VirginBijak" i:nil="true" />
                   </EntityNested>
                 </ArrayOfEntityNested>'

我需要将XML中的数据插入到临时表中。

here

为此,我使用以下代码。但它不起作用,并且它没有在临时表中插入任何数据。

--Variables Decleration
DECLARE @XMLData VARCHAR(MAX)
DECLARE @idoc INT

-- Creating Temporary Table
CREATE TABLE #TEMP_TABLE
(
    REC_ID INT IDENTITY(1,1),
    [Id] INT,
    [Date] VARCHAR(50),
    [Number] VARCHAR(50),
);

--Case 1
SET @XMLData = '<ArrayOfEntityNested xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
                                     xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto.Bijak">
                   <EntityNested>
                      <Id xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto">1</Id>
                      <Date xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto.VirginBijak">0001-01-01T00:00:00</Date>
                      <Number xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto.VirginBijak" i:nil="true" />
                   </EntityNested>
                </ArrayOfEntityNested>
                               '
--Reading Data from XML and inserting into Temp Table
EXECUTE sp_xml_preparedocument @idoc OUTPUT, @XMLData

INSERT INTO #TEMP_TABLE
   SELECT * 
   FROM OpenXML(@idoc,'/ArrayOfEntityNested/EntityNested', 1)
        WITH #TEMP_TABLE

EXECUTE sp_xml_removedocument @idoc

--Displaying data from Temp Table
SELECT * FROM #TEMP_TABLE
DROP TABLE #TEMP_TABLE;

但如果xml格式正确可能如下:

,则无效
SET @XMLData = '<ArrayOfEntityNested>
                   <EntityNested>
                      <Id>1</Id>
                      <Date>0001-01-01T00:00:00</Date>
                      <Description>deesc</Description>
                      <EmployeeId>2</EmployeeId>
                      <IsDeleted>false</IsDeleted>
                      <LoadingPermitTruckId>7541</LoadingPermitTruckId>
                    </EntityNested>
                 </ArrayOfEntityNested>'

然后它有效。

请帮帮我。

3 个答案:

答案 0 :(得分:5)

首先 - 请使用适当的数据类型!如果您的源数据是XML - 为什么不使用XML数据类型?

此外,如果您的表中有Date - 为什么不是DATEDATETIME类型?为什么Number一个VARCHAR(50) ??

没有意义......

然后:您没有查看XML文档中存在的XML命名空间 - 但必须

最后 - 我建议使用原生XQuery支持,而不是旧版,已弃用sp_xml_preparedocument / OpenXML方法......

对我来说似乎更容易,更清楚......

使用此:

-- variable declaration
DECLARE @XMLData XML

-- creating temporary table
CREATE TABLE #TEMP_TABLE
(
    REC_ID INT IDENTITY(1,1),
    [Id] INT,
    [Date] DATETIME2(3),
    [Number] INT
); 

然后使用适当的XQuery语句包括XML命名空间来处理数据:

SET @XMLData = '<ArrayOfEntityNested xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
                                 xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto.Bijak">
                    <EntityNested>
                       <Id xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto">1</Id>
                       <Date xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto.VirginBijak">0001-01-01T00:00:00</Date>
                       <Number xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto.VirginBijak" i:nil="true" />
                    </EntityNested>
                    <EntityNested>
                       <Id xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto">42</Id>
                       <Date xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto.VirginBijak">2013-12-22T14:45:00</Date>
                       <Number xmlns="http://schemas.datacontract.org/2004/07/Gbms.Dto.VirginBijak">373</Number>
                    </EntityNested>
                 </ArrayOfEntityNested>'

;WITH XMLNAMESPACES ('http://schemas.datacontract.org/2004/07/Gbms.Dto.Bijak' AS ns1,
                'http://schemas.datacontract.org/2004/07/Gbms.Dto' AS ns2,
                'http://schemas.datacontract.org/2004/07/Gbms.Dto.VirginBijak' AS ns3)
INSERT INTO #TEMP_TABLE(ID, Date, Number)
   SELECT
  xc.value('(ns2:Id)[1]', 'int'),
  xc.value('(ns3:Date)[1]', 'DateTime2'),
  xc.value('(ns3:Number)[1]', 'int')
   FROM 
  @XmlData.nodes('/ns1:ArrayOfEntityNested/ns1:EntityNested') AS xt(xc)

答案 1 :(得分:1)

DECLARE @idoc int

DECLARE @doc varchar(1000)

SET @doc ='
<OutLookContact>
<Contact FirstName="Asif" LastName="Ghafoor" EmailAddress1="asifghafoor@my.web.pk" />
<Contact FirstName="Rameez" LastName="Ali" EmailAddress1="rameezali@my.web.pk" />
</OutLookContact>'

--Create an internal representation of the XML document.

EXEC sp_xml_preparedocument @idoc OUTPUT, @doc

-- Execute a SELECT statement that uses the OPENXML rowset provider.

DECLARE @Temp TABLE(FirstName VARCHAR(250),LastName VARCHAR(250),Email1 VARCHAR(250))  

INSERT INTO @Temp(FirstName,LastName,Email1)



SELECT *

FROM OPENXML (@idoc, '/OutLookContact/Contact',1)

WITH (FirstName varchar(50),LastName varchar(50),EmailAddress1 varchar(50))


select FirstName,LastName,Email1 from @Temp

答案 2 :(得分:0)

如果你尝试使用名为pentaho的工具,那将会容易得多。 http://en.wikipedia.org/wiki/Pentaho 它是一个用于数据集成的开源工具。你可以创建一个从mysql或oracle到它的数据库连接并进行转换。它很容易使用。