August 18, 2010 at 7:07 am
Is there any way to import XML below into a formatted table so the table looks something like (Sub,CUSID,ISSN,YEAR,RATE)
Thanks
Here is the XML
------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://services.myservice.com">
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Sub">
<xs:complexType>
<xs:sequence>
<xs:element name="CUSID" type="xs:string" minOccurs="0"/>
<xs:element name="ISSN" type="xs:string" minOccurs="0"/>
<xs:element name="YEAR" type="xs:string" minOccurs="0"/>
<xs:element name="RATE" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Sub diffgr:id="Sub1" msdata:rowOrder="0">
<CUSID>90000024</CUSID>
<ISSN>00131350</ISSN>
<YEAR>2005</YEAR>
<RATE>AS</RATE>
</Sub>
<Sub diffgr:id="Sub2" msdata:rowOrder="1">
<CUSID>90000024</CUSID>
<ISSN>03060012</ISSN>
<YEAR>2005</YEAR>
<RATE>AS</RATE>
</Sub>
<Sub diffgr:id="Sub3" msdata:rowOrder="2">
<CUSID>90000024</CUSID>
<ISSN>03060012</ISSN>
<YEAR>2006</YEAR>
<RATE>AS</RATE>
</Sub>
<Sub diffgr:id="Sub4" msdata:rowOrder="3">
<CUSID>90000024</CUSID>
<ISSN>03060012</ISSN>
<YEAR>2007</YEAR>
<RATE>AS</RATE>
</Sub>
<Sub diffgr:id="Sub5" msdata:rowOrder="4">
<CUSID>90000024</CUSID>
<ISSN>03060012</ISSN>
<YEAR>2008</YEAR>
<RATE>AS</RATE>
</Sub>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
August 18, 2010 at 8:23 am
You would need to read the XML and split into rows. There is a task in SSIS that can help here. Is this a one time thing or a repeatable process?
August 18, 2010 at 8:23 am
Sorry, forgot to include a link to help: http://www.sqlservercentral.com/Forums/Topic854028-148-1.aspx
August 18, 2010 at 9:07 am
Thanks for reply. It will be an ongoing process (XML data will be process daily). I'll check the link.
Thanks
August 18, 2010 at 9:09 am
I was hoping tu use OPENXML and T-SQL. It will ebnice to create the whole impor tprocess as a stored procedure. Hmmm. Anyone can help with OPENXML?
August 18, 2010 at 10:19 am
I would use XQuery over OpenXML.
However, I had to change your sample data:
Your row < NewDataSet xmlns="" > will result in an XQuery error since you need to use valid namespaces once you started using it. If the namespace gets resetted in the middle of the xml file it'll throw an error. Therefore, I changed it to < NewDataSet xmlns="http://services.myservice.com" >
DECLARE @xml XML
SET @xml='your xml data'
;WITH XMLNAMESPACES
(
'urn:schemas-microsoft-com:xml-diffgram-v1' AS diffgr,
'urn:schemas-microsoft-com:xml-msdata' AS msdata,
DEFAULT 'http://services.myservice.com'
)
SELECT
c.value('@diffgr:id[1]','varchar(10)') AS Sub,
c.value('CUSID[1]','varchar(10)') AS CUSID,
c.value('ISSN[1]','varchar(10)') AS ISSN,
c.value('YEAR[1]','INT') AS YEAR,
c.value('RATE[1]','varchar(10)') AS RATE
FROM @xml.nodes('DataSet/diffgr:diffgram/NewDataSet/Sub') T(c)
/* result set:
SubCUSIDISSNYEARRATE
Sub190000024001313502005AS
Sub290000024030600122005AS
Sub390000024030600122006AS
Sub490000024030600122007AS
Sub590000024030600122008AS
*/
August 19, 2010 at 2:36 am
Hall of Fame,
Thanks for your help. It is working with XQUERY and that is fantastic. You saved me probably a week and more work on this XML import.
Thanks again.
August 19, 2010 at 2:48 am
Hi Hall Of Fame, again me. Sorry to bother you again, can you help with the last XML file to import it into SQL table.
Here is the file:
--------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://services.myservice.com">
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Customers">
<xs:complexType>
<xs:sequence>
<xs:element name="CUSID" type="xs:string" minOccurs="0" />
<xs:element name="ORGNAME" type="xs:string" minOccurs="0" />
<xs:element name="CITY" type="xs:string" minOccurs="0" />
<xs:element name="COUNTRY" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Customers diffgr:id="Customers1" msdata:rowOrder="0">
<CUSID>10000024</CUSID>
<ORGNAME>TEST1 UNIVERSITY</ORGNAME>
<CITY>Coventry</CITY>
<COUNTRY>United Kingdom</COUNTRY>
</Customers>
<Customers diffgr:id="Customers2" msdata:rowOrder="1">
<CUSID>10000082</CUSID>
<ORGNAME>TEST2 UNIVERSITY</ORGNAME>
<CITY>Gif Sur Yvette Cedex</CITY>
<COUNTRY>France</COUNTRY>
</Customers>
<Customers diffgr:id="Customers3" msdata:rowOrder="2">
<CUSID>10000087</CUSID>
<ORGNAME>TEST3 UNIVERSITY</ORGNAME>
<CITY>Baltimore</CITY>
<COUNTRY>United States of America</COUNTRY>
</Customers>
<Customers diffgr:id="Customers4" msdata:rowOrder="3">
<CUSID>10000088</CUSID>
<ORGNAME>TEST4 UNIVERSITY</ORGNAME>
<CITY>Kansas City</CITY>
<COUNTRY>United States of America</COUNTRY>
</Customers>
<Customers diffgr:id="Customers5" msdata:rowOrder="4">
<CUSID>10000133</CUSID>
<ORGNAME>TEST5 UNIVERSITY</ORGNAME>
<CITY>Bangalore</CITY>
<COUNTRY>India</COUNTRY>
</Customers>
<Customers diffgr:id="Customers6" msdata:rowOrder="5">
<CUSID>10000158</CUSID>
<ORGNAME>TEST5 UNIVERSITY</ORGNAME>
<CITY>Shatin</CITY>
<COUNTRY>Hong Kong</COUNTRY>
</Customers>
<Customers diffgr:id="Customers7" msdata:rowOrder="6">
<CUSID>10000162</CUSID>
<ORGNAME>TEST6 UNIVERSITY</ORGNAME>
<CITY>Glasgow</CITY>
<COUNTRY>United Kingdom</COUNTRY>
</Customers>
<Customers diffgr:id="Customers8" msdata:rowOrder="7">
<CUSID>10000182</CUSID>
<ORGNAME>TEST6 UNIVERSITY</ORGNAME>
<CITY>Monroeville</CITY>
<COUNTRY>United States of America</COUNTRY>
</Customers>
<Customers diffgr:id="Customers9" msdata:rowOrder="8">
<CUSID>10000197</CUSID>
<ORGNAME>TEST7 UNIVERSITY</ORGNAME>
<CITY>Oldenburg</CITY>
<COUNTRY>Germany</COUNTRY>
</Customers>
<Customers diffgr:id="Customers10" msdata:rowOrder="9">
<CUSID>10000214</CUSID>
<ORGNAME>TEST8 UNIVERSITY</ORGNAME>
<CITY>Wetherby</CITY>
<COUNTRY>United Kingdom</COUNTRY>
</Customers>
<Customers diffgr:id="Customers11" msdata:rowOrder="10">
<CUSID>10000222</CUSID>
<ORGNAME>TEST9 UNIVERSITY</ORGNAME>
<CITY>New Haven</CITY>
<COUNTRY>United States of America</COUNTRY>
</Customers>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
August 19, 2010 at 6:13 am
Hi Forum Newbie ( )
At a first glance: it's the same issue like the first file:
There is a line where the default namespace is reset to an empty string: < NewDataSet xmlns="" >
Change that to either < NewDataSet xmlns="http://services.myservice.com" > or < NewDataSet >.
After that you just would need to adjust the previous query to match the new node names. Give it a try and post back where you get stuck.
August 19, 2010 at 9:38 am
Thanks again. I got it all working as you suggested. Thansk for quick repaly. That was very helpful.
Thanks again for your time.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy