insert data from xml column into temp table

后端 未结 3 1110

I have a xml column that look like

SET @XMLData = \'

        
相关标签:
3条回答
  • 2021-01-07 03:17

    First of all - please use appropriate data types! If your source data is XML - why aren't you using the XML datatype?

    Also, if you have a Date in your table - why isn't that a DATE or DATETIME type?? And why is the Number a VARCHAR(50) ??

    Makes no sense......

    Then: you're not looking at the XML namespaces that are present in the XML document - but you must!

    At lastly - I would recommend using the native XQuery support instead of the legacy, deprecated sp_xml_preparedocument / OpenXML approach....

    Seems much easier, much clearer to me...

    Use this:

    -- 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
    ); 
    

    and then use proper XQuery statements, including the XML namespaces to handle the data:

    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)
    
    0 讨论(0)
  • 2021-01-07 03:37

    it will be a lot easier if you try to use a tool called pentaho. http://en.wikipedia.org/wiki/Pentaho it is an open source tool which is used for data integration.you can create a database connection from mysql or oracle to it and do the transformation.it is easy to use.

    0 讨论(0)
  • 2021-01-07 03:38
    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
    
    0 讨论(0)
提交回复
热议问题