• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 37
  • Last Modified:

Only getting one result from an xml extraction in an MS SQL stored procedure

I have an xml file which I store to @xml in a MS Sql stored procedure.  I execute on the @xml,  yet the only value I am returning is DBP1 of 118.  Here is the xml

<PCRDATA>
<PCRDATA>
	<Activity>
		<Events>
                      <EVENT>
			<BloodPressureDiastolic>118</BloodPressureDiastolic>
			<BloodPressureSystolic>212</BloodPressureSystolic>
		</EVENT>
                     <EVENT>
			<BloodPressureDiastolic>90</BloodPressureDiastolic>
			<BloodPressureSystolic>180</BloodPressureSystolic>
                      </EVENT>
		</EVENTS>
	</Activity>
</PCRDATA>

Open in new window


Here is the syntax I am using:
@xml.value(''(/PCRDATA/ACTIVITY/EVENTS/EVENT/BloodPressureSystolic)[1]'', '' Varchar(10)'' ) AS SBP1,
@xml.value(''(/PCRDATA/ACTIVITY/EVENTS/EVENT/BloodPressureDiastolic)[1]'', '' Varchar(10)'' ) AS DBP1,
@xml.value(''(/PCRDATA/ACTIVITY/EVENTS/EVENT/BloodPressureSystolic)[2]'', '' Varchar(10)'' ) AS SBP1,
@xml.value(''(/PCRDATA/ACTIVITY/EVENTS/EVENT/BloodPressureDiastolic)[2]'', '' Varchar(10)'' ) AS DBP1

Open in new window



is my approach with the sub nodes incorrect or do I also Have to refer to a sub node for EVENT

Thanks
0
Will
Asked:
Will
  • 4
  • 3
1 Solution
 
Máté FarkasDatabase Developer and AdministratorCommented:
You have to use this:
1: @xml.value(''(/PCRDATA/ACTIVITY/EVENTS[1]/BloodPressureSystolic)[1]'', '' Varchar(10)'' ) AS SBP1,
2: @xml.value(''(/PCRDATA/ACTIVITY/EVENTS[1]/BloodPressureDiastolic)[1]'', '' Varchar(10)'' ) AS DBP1,
3: @xml.value(''(/PCRDATA/ACTIVITY/EVENTS[2]/BloodPressureSystolic)[1]'', '' Varchar(10)'' ) AS SBP1,
4: @xml.value(''(/PCRDATA/ACTIVITY/EVENTS[2]/BloodPressureDiastolic)[1]'', '' Varchar(10)'' ) AS DBP1

Open in new window

0
 
WillOwnerAuthor Commented:
Thank you for the response

I suspected so:  so I tried this
@xml.value(''(/PCRDATA/ACTIVITY/EVENTS/EVENT[1]/BloodPressureSystolic)[1]'', '' Varchar(10)'') AS SBP1,
@xml.value(''(/PCRDATA/ACTIVITY/EVENTS/EVENT[2]/BloodPressureSystolic)[2]'', '' Varchar(10)'' ) AS SBP2,
@xml.value(''(/PCRDATA/ACTIVITY/EVENTS/EVENT[1]/BloodPressureDiastolic)[1]'', '' Varchar(10)'' ) AS DBP1,
@xml.value(''(/PCRDATA/ACTIVITY/EVENTS/EVENT[2]/BloodPressureDiastolic)[2]'', '' Varchar(10)'' ) AS DBP2

Open in new window


Only pulls DBP1 as 118
0
 
Máté FarkasDatabase Developer and AdministratorCommented:
Sorry,
Actually your xml is not correct. This would be the correct XML:
<PCRDATA>
	<Activity>
		<EVENTS>
                      <EVENT>
			<BloodPressureDiastolic>118</BloodPressureDiastolic>
			<BloodPressureSystolic>212</BloodPressureSystolic>
		     </EVENT>
                     <EVENT>
			<BloodPressureDiastolic>90</BloodPressureDiastolic>
			<BloodPressureSystolic>180</BloodPressureSystolic>
                      </EVENT>
		</EVENTS>
	</Activity>
</PCRDATA>

Open in new window


You should use this query instead:
SELECT  n.value('BloodPressureDiastolic[1]', ' Varchar(10)' ) AS DBP,
        n.value('BloodPressureSystolic[1]', ' Varchar(10)' ) AS SBP
FROM @xml.nodes('PCRDATA/Activity/EVENTS/EVENT') x(n)

Open in new window


Important: XQuery is case sensitive (make sure that opening node and closing node have the same casing and all nodes must have the same casing) and you must use the same casing in XQuery as the nodes have in xml.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
WillOwnerAuthor Commented:
Great.  Thank you.  Turned out to be the case sensitivity as well
1
 
WillOwnerAuthor Commented:
Thank you Máté .
1
 
Máté FarkasDatabase Developer and AdministratorCommented:
You are welcome.
Please accept my solution.
0
 
WillOwnerAuthor Commented:
Great work
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now