Solved

Parse XML in SQL

Posted on 2011-03-24
2
383 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

813 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

16 Experts available now in Live!

Get 1:1 Help Now