Solved

Parse XML in SQL

Posted on 2011-03-24
2
376 Views
Last Modified: 2012-05-11
In my (partial) attached code it works...and I can output my table...but it's only picking up the FIRST  <ErrorText> content.
I need to create two identical lines of data with the ErrorText line different

I Execute it with this code
Declare @info nvarchar(300)
Set @info = 'Errors'
Declare @xml xml
Set @xml = '<?xml version="1.0" encoding="UTF-8" ?>
  <Document>
  <XMLVersion version="1.00" />
  <Record>
  <PacketID>0000410403584837143000</PacketID>
  <PacketSequence>1</PacketSequence>
  <Ref1 />
  <Ref2 />
  <Ref3 />
  <Ref4 />
  <LienRefNum />
  <OrderNumber />
  <Status type="Error" />
  <Errors>
  <ErrorText>Invalid XML Document</ErrorText>
  <ErrorText>The element Names has invalid child element IndividualName. List of possible elements expected: MailAddress.</ErrorText>
  </Errors>
  </Record>
  </Document>'

exec sp_portal_UCC_ProcessStatusReplies_BU_LB_0324 @info, @xml
ALTER PROCEDURE [dbo].[sp_portal_UCC_ProcessStatusReplies_BU_LB_0324] (@xmlRequestInfo varchar(300), @xmlData xml)
AS
BEGIN
--============================================================================
--Create Temp Table and insert data
--============================================================================
	CREATE TABLE #XmlImport   
		(    
			xmlFileInfo VARCHAR(300) NOT NULL,    
			xml_data XML NOT NULL    
		) 
		
	Insert into #XmlImport
       (xmlFileInfo,xml_data)
	Select @xmlRequestInfo, @xmlData
--============================================================================
--Process Data for Select
--============================================================================
 -- variable to store the XML content
	declare @xml_text varchar(max)

 -- xml document handle             
	declare @i int                                         
  
-- get XML content from XML data type field in table into a variable  
	select @xml_text = cast(xml_data as varchar(max)) from #XmlImport  
  
-- prepare the XML document  
	exec sp_xml_preparedocument @i OUTPUT, @xml_text  

-- Reply variable
	declare @response nvarchar(200)
	set @response = @xmlRequestInfo + '-Done'

select @response as sqlReply, * from  
openxml(@i,'/Document/Record',2)  
with  
(  		
	 PacketID nvarchar(100)  
	,PacketSequence nvarchar(10)
	,Ref1 nvarchar(40) 'Ref1'
	,Ref2 nvarchar(2000) 'Ref2'
	,Ref3 nvarchar(200) 'Ref3'
	,Ref4 nvarchar(40) 'Ref4'
	,LienRefNum nvarchar(10) 'LienRefNum'
	,OrderNumber nvarchar(40) 'OrderNumber'
	,ErrorText nvarchar(1000) 'Errors/ErrorText'

) s


End

Open in new window

0
Comment
Question by:lrbrister
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 35211630
alter PROCEDURE [dbo].[sp_portal_UCC_ProcessStatusReplies_BU_LB_0324] (@xmlRequestInfo varchar(300), @xmlData xml)
AS
BEGIN
-- Reply variable
	declare @response nvarchar(200)
	set @response = @xmlRequestInfo + '-Done'

select @response as sqlReply,
	q.n.value('PacketID[1]', 'nvarchar(100)') PacketID,
	q.n.value('PacketSequence[1]', 'nvarchar(10)') PacketSequence,
	q.n.value('Ref1[1]', 'nvarchar(40)') Ref1,
	q.n.value('Ref2[1]', 'nvarchar(2000)') Ref2,
	q.n.value('Ref3[1]', 'nvarchar(200)') Ref3,
	q.n.value('Ref4[1]', 'nvarchar(40)') Ref4,
	q.n.value('LienRefNum[1]', 'nvarchar(10)') LienRefNum,
	q.n.value('OrderNumber[1]', 'nvarchar(40)') OrderNumber,
	e.t.value('.[1]', 'nvarchar(1000)') [Errors/ErrorText]
from @xmlData.nodes('/Document/Record') q(n)
outer apply q.n.nodes('Errors/ErrorText') e(t)
End
go

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 35214159
Perfect...thanks.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now