darth continent's Avatar
darth continent 4
40 Asked
593 Answered
160 Best
0
No one has voted on this question yet :(
3 years, 4 months ago

In MS SQL 2005, is there a way to determine whether a specific node exists in an XML variable?

For example, say I have the following XML:

.... [parentNode]

........ [test1][/test1]
........ [test2][/test2]

........ [subNode]
............ [subTest1]123[/subTest1]
............ [subTest2]234[/subTest2]
........ [/subNode]

.... [/parentNode]

Once I load the XML into a variable, there will be instances when [subNode] may or may not exist. What's a straightforward method to detect whether [subNode] is present?
Tip for best answer: M$1.00
Separate topics with commas, or by pressing return. Use the delete or backspace key to edit or remove existing topics.

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$

What is Your Answer?

0
0
0

2 Answers

0
ilaksh's Avatar
ilaksh | 3 years, 4 months ago
4
Wait, I thought you were doing it in 2000? Anyway, something like this:

RETURN (SELECT @xmlData.exist('/parentNode/subNode'))

you might have to mess with the xpath query a little depending on the exact structure but I am pretty sure something like that will work.
source(s):
http://msdn.microsoft.com/en-us/library/ms345117.aspx (under heading Example: Constraining XML Column)

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$
darth continent's Avatar
darth continent | 3 years, 4 months ago Report

Thanks; this issue actually I can use the features of 2005 with. The one related to the other question I can't because of time constraints; the time it'd take to change all the underlying code to use 2005's better XML functionality will have to wait until a deadline of mine has passed.

Report Abuse

Post Reply Cancel
1
dotnetallday's Avatar
dotnetallday | 3 years, 4 months ago
4
It appears that SQL supports XML queries, but I would do this type of query on the application level. Here's an interesting article on SQL and XML:

http://www.15seconds.com/Issue/050803.htm

You can leave an optional "tip" with Mahalo's virtual currency, Mahalo Dollars. If you are asking a difficult question that might require some research, or if you'd like a wide variety of feedback, a higher tip often leads to more answers to your question.

M$

Report Abuse

Post Reply Cancel

Learn something new with our FREE educational apps!

Private lessons in the comfort of your own home. Get back in shape or finally pick up a guitar with our great experts guiding you the whole way!
Learn Guitar
Learn Hip Hop
Learn Pilates