Read XMl file to SQL table

  • I am trying to import xml file to sql table. Any help please! m new with XML, obviously!

    Sample XML file

    <CustomerDetails>

    <PersonalInfo>

    <CustID>1001</CustID>

    <CustLastName>Smith</CustLastName>

    <DOB>2011-05-05T09:25:48.253</DOB>

    <Address>

    <Addr1>100 Smith St.</Addr1>

    <City>New York</City>

    </Address>

    </PersonalInfo>

    </CustomerDetails>

    And the result table should look like

    CREATE TABLE #Cust

    (CustID INT, CustLastName VARCHAR(10)

    , DOB DATETIME, Addr1 VARCHAR(100), City VARCHAR(10))

    INSERT INTO #Cust

    VALUES (1001, 'Smith', '2011-05-05', '100 Smith St.', 'New York')

  • Replace the C:\test.txt with your filename in the following.

    insert into #Cust

    select

    c3.value('CustID[1]','int'),

    c3.value('CustLastName[1]','varchar(10)'),

    c3.value('DOB[1]','DATETIME'),

    c3.value('(Address/Addr1)[1]','VARCHAR(100)'),

    c3.value('(Address/City)[1]','VARCHAR(10)')

    from

    (

    select

    cast(c1 as xml)

    from

    OPENROWSET (BULK 'C:\test.txt',SINGLE_BLOB) as T1(c1)

    )as T2(c2)

    cross apply c2.nodes('/CustomerDetails/PersonalInfo') T3(c3)

  • thanks much!

  • Hi Can u plz explain the code i cant get

  • thx

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply