简体   繁体   中英

SQL Server equivalent of MySQL Load xml infile

I have been using the MySQL Load XML Infile to load an xml file into a MySQL tables. The xml file has this format

<Detail_Collection>
    <Detail
        JobId=“12345”
        JobDescription=“Job1”
        Sold_To=“Customer1”
    />
    <Detail
        JobId=“23445”
        JobDescription=“Job2”
        Sold_To=“Customer2”
    />
</Detail_Collection>

My table looks like this

JobId   
JobDescription   
Sold_to   

Every thing works fine. I am finding myself having to move from MySQL to SQL Server and can't seem to find a simple way to do this in SQL Server. Am I missing something

If you are looking for a SQL query to fetch the job data from your XML file into SQL Server database table, please check the below T-SQL syntax for querying XML data on SQL Server

declare @xml as xml
set @xml = '
<Detail_Collection>
    <Detail
        JobId="12345"
        JobDescription="Job1"
        Sold_To="Customer1"
    />
    <Detail
        JobId="23445"
        JobDescription="Job2"
        Sold_To="Customer2"
    />
</Detail_Collection>'
select @xml

select
 job.value('@JobId','int') JobId,
 job.value('@JobDescription','varchar(400)') JobDescription,
 job.value('@Sold_To','varchar(400)') Sold_To
from @xml.nodes('/Detail_Collection/Detail') as jobs(job)

You can find tutorials on querying XML on SQL Server at given reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM