sql - XML input into a temp table -
I am new to using XML inside SQL Server and for whatever I want it is a bit confusing I am doing I have tried to work some examples from around the site, but I need it, but there is no luck, any help my Quick XML will be appreciated:
& Lt ;? Xml version = "1.0" encoding = "utf-8" & gt; & Lt; Service xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/2001/XMLSchema" & gt; & Lt; MParentServiceServiceID & gt; 37,694 & lt; / MServiceID & gt; & Lt; MJourneyMessage & gt; Canceled & lt; / MJourneyMessage & gt; & Lt; MApplicableDate & gt; 2014-10-10 & lt; / MApplicableDate & gt; & Lt; Edits & gt; & Lt; MServiceID & gt; 14,466 & lt; / MServiceID & gt; & Lt; MStopSequenceOrdinal & gt; 2 & lt; / MStopSequenceOrdinal & gt; & Lt; / Edits & gt; & Lt; Edits & gt; & Lt; MServiceID & gt; 14,466 & lt; / MServiceID & gt; & Lt; MStopSequenceOrdinal & gt; 3 & lt; / MStopSequenceOrdinal & gt; & Lt; / Edits & gt; & Lt; Edits & gt; & Lt; MServiceID & gt; 14467 & lt; / MServiceID & gt; & Lt; MStopSequenceOrdinal & gt; 5 & lt; / MStopSequenceOrdinal & gt; & Lt; / Edits & gt; & Lt; Edits & gt; & Lt; MServiceID & gt; 14467 & lt; / MServiceID & gt; & Lt; MStopSequenceOrdinal & gt; 4 & lt; / MStopSequenceOrdinal & gt; & Lt; / Edits & gt; & Lt; Edits & gt; & Lt; MServiceID & gt; 14468 & lt; / MServiceID & gt; & Lt; MStopSequenceOrdinal & gt; 4 & lt; / MStopSequenceOrdinal & gt; & Lt; / Edits & gt; & Lt; / MEdits & gt; & Lt; / Services & gt; that is being sent in an archived procedure through an XML variable, which is given @ Description
I would like to see my table like the following
mParentServiceID | MJourneyMessage | MApplicableDate | MServiceID | MStopsSequenceOrdinal 37,694 cancellation 2014-10-10 14,466 2 37,694 cancellation 2014-10-10 14466 3 37694 2014-10-10 14467 5 37694 cancellation cancel 2014-10-10 14467 4 37,694 canned 2014-10-10 14468 4 I know this is not normalized but it is a loft table, so that I can manipulate a little data before storing in the main table
till now i Select to enter in
@ApplicableServices OPENXML (@Details, '/ service /', 2) (mParentServiceServiceID is with integer * '../@SmParentServiceServiceID', mJourneyMessage varchar (30) '.. / @ MjourneyMessage ', mApplicableDate Date' .../@mApplicableDate ', mServiceID Ic class = "post-text" itemprop = "Integer '@mServiceID', mStopSequenceOrdinal integer '@mStopSequenceOrdinal') Many thanks
Try it (by the way, xml had some syntax errors, so I had to make some changes, so I've listed it out in full):
DECLARE @ XML XML = '& lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Service xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" & gt; & Lt; MParentServiceServiceID & gt; 37,694 & lt; / MParentServiceServiceID & gt; & Lt; MJourneyMessage & gt; Canceled & lt; / MJourneyMessage & gt; & Lt; MApplicableDate & gt; 2014-10-10 & lt; / MApplicableDate & gt; & Lt; Edits & gt; & Lt; MServiceID & gt; 14,466 & lt; / MServiceID & gt; & Lt; MStopSequenceOrdinal & gt; 2 & lt; / MStopSequenceOrdinal & gt; & Lt; / Edits & gt; & Lt; Edits & gt; & Lt; MServiceID & gt; 14,466 & lt; / MServiceID & gt; & Lt; MStopSequenceOrdinal & gt; 3 & lt; / MStopSequenceOrdinal & gt; & Lt; / Edits & gt; & Lt; Edits & gt; & Lt; MServiceID & gt; 14467 & lt; / MServiceID & gt; & Lt; MStopSequenceOrdinal & gt; 5 & lt; / MStopSequenceOrdinal & gt; & Lt; / Edits & gt; & Lt; Edits & gt; & Lt; MServiceID & gt; 14467 & lt; / MServiceID & gt; & Lt; MStopSequenceOrdinal & gt; 4 & lt; / MStopSequenceOrdinal & gt; & Lt; / Edits & gt; & Lt; Edits & gt; & Lt; MServiceID & gt; 14468 & lt; / MServiceID & gt; & Lt; MStopSequenceOrdinal & gt; 4 & lt; / MStopSequenceOrdinal & gt; & Lt; / Edits & gt; & Lt; / Services & gt; 'Select Serv.Edit.value (' ../mParentServiceServiceID [1] ',' INT ') mParentServiceServiceID, Serv.Edit.value (' ../mJourneyMessage [1] ',' VARCHAR (50) ') mJourneyMessage, all . Edit.value ('../mApplicableDate [1]', 'date') mApplicableDate, Serv.Edit.value ('mServiceID [1]', 'INT') mServiceID, Serv.Edit.value ('mStopSequenceOrdinal [1] ',' INT ') mStopSequenceOrdinal FROM @ XML.nodes (' / edit ') AS serv (edit)
Comments
Post a Comment