Solved

Parse XML in SQL

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

920 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

20 Experts available now in Live!

Get 1:1 Help Now