Solved

Parse XML in SQL

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

860 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