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
LVL 5
WillOwnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.