How to correct the XPath Query in OpenXML statement to get the state for each row.

Posted on 2010-08-25
Last Modified: 2012-05-10
I am having trouble pulling the "StateProvCd" value for each RatingClassificationCd in the following script.  Have been playing with the Xpath and it always returns "IN" for both rows instead of "IN" for the first row and "IL" for the second row.

DECLARE @idoc int

EXEC sp_xml_preparedocument @idoc output, '<MainElement>

		<LineBusiness id="12345">





			<RateClass id="abc">



			<RatingClassificationDesc>CLASS TEST 1</RatingClassificationDesc>







			<RateClass id="def">



  			<RatingClassificationDesc>CLASS TEST 2</RatingClassificationDesc>  






SELECT RatingClassificationCd, RatingClassificationDesc, Exposure, StateProvCd

FROM   OPENXML (@idoc, '/MainElement/LineBusiness/RateState/LocInfo/RateClass', 3)

						WITH (StateProvCd				char(2) '../../../RateState/StateProvCd',

							 RatingClassificationCd 		char(5) 'RatingClassificationCd', 

							 RatingClassificationDesc 	varchar(250) 'RatingClassificationDesc',

							 Exposure					numeric(18,0) 'Exposure'							 


EXEC sp_xml_removedocument @idoc

Open in new window

Question by:EIHI-IT
  • 2

Accepted Solution

grepll earned 500 total points
ID: 33521907
This should work:
WITH (StateProvCd char(2) 'ancestor::RateState/StateProvCd',

Open in new window


Author Comment

ID: 33521947
Thank You!

Expert Comment

ID: 33522045

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

911 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

23 Experts available now in Live!

Get 1:1 Help Now