SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XML XQuery problem


XML XQuery problem

Author
Message
Marcin Zawadzki
Marcin Zawadzki
SSC Veteran
SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)

Group: General Forum Members
Points: 286 Visits: 79
Comments posted to this topic are about the item XML XQuery problem
ChiragNS
ChiragNS
SSCrazy Eights
SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)SSCrazy Eights (8.4K reputation)

Group: General Forum Members
Points: 8433 Visits: 1865
Why 4 points for this question? I mean u can run this thing in SSMS and get the answer.

"Keep Trying"
Marcin Zawadzki
Marcin Zawadzki
SSC Veteran
SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)

Group: General Forum Members
Points: 286 Visits: 79
Do not now - I'am only author of this question, others are responsible for give a score..
Campden BRI
Campden BRI
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1588 Visits: 206
XML using T-SQL would be less of a pain if it did allow variables
SuperDBA-207096
SuperDBA-207096
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3345 Visits: 711
I think it's a pretty good question, would like to see more xml questions!
Steve Jones
Steve Jones
SSC Guru
SSC Guru (185K reputation)SSC Guru (185K reputation)SSC Guru (185K reputation)SSC Guru (185K reputation)SSC Guru (185K reputation)SSC Guru (185K reputation)SSC Guru (185K reputation)SSC Guru (185K reputation)

Group: Administrators
Points: 185614 Visits: 19504
If you want to run it in SSMS and get the answer, that's up to you. However that kind of defeats the purpose of the questions. You should make an attempt to answer the question without running it.

Think about it, if someone asked you this in an interview, would you answer it or say, "I can't do it without SSMS?"

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
StarNamer
StarNamer
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3228 Visits: 1992
Did this actually appear as intended?

SET @xml =
'
'



I would have thought there should be something between the quotes.

Derek
kevin77
kevin77
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2300 Visits: 1102
Could someone give me a code example of this that does work. I tried:


DECLARE @xml xml;
DECLARE @expression nvarchar(128);
SET @xml =
'<books>
<book price=''31'' name=''SQL Server Central'' Author=''Steve Jones''/>
</books>';

--SET @expression = '/books/book/@price[text()=31]';


IF @xml.exist('/books/book/@price[text()=31]') =1
SELECT
T.value('./@price[1]','int'),
T.value('./@name[1]','nvarchar(32)'),
T.value('./@Author[1]','nvarchar(32)')
FROM @xml.nodes('/books/book') AS T(T);



and I get the error:

XQuery [exist()]: Result of 'text()' expression is statically 'empty'

So I replace the XPath expression with:

/books/book/@price=31



and it works.

I don't use Xml and XPath every day, but I understand most of the basic concepts. What should the Xml look like in order to use the original XPath expression? The '@price' means an attribute and 'text()' all the text within a node. To me, the XPath expression is invalid or just doesn't make sense.

P.S.
Did the author just do the same thing I did and forget to escape out the '<' with '& lt;' in the Xml string when writing and posting the question?

P.P.S
I can't get an escaped ampersand followed by 'lt;', as in above to show up in these forums without manually putting that space between them.



RBarryYoung
RBarryYoung
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45160 Visits: 9518
Derek Dongray (3/12/2009)
Did this actually appear as intended?

SET @xml =
'
'



I would have thought there should be something between the quotes.

Actually, I think that was the toughest part of this question. I spent 5 minutes thinking about whether or not this was another case of the XML getting eaten by this site before I risked the 4 points by trying to answer it! (maybe that's why it 4 points? Smile )

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
RBarryYoung
RBarryYoung
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45160 Visits: 9518
Steve Jones - Editor (3/12/2009)
If you want to run it in SSMS and get the answer, that's up to you. However that kind of defeats the purpose of the questions. You should make an attempt to answer the question without running it.

Think about it, if someone asked you this in an interview, would you answer it or say, "I can't do it without SSMS?"

FWIW, I do not think that questions of the form "What would you get if you ran this in SSMS" are the best choice, either here or in an interview.

In fact I would never ask a question like that in an interview unless I wanted them to say "Try it and see." I already have a resource that can tell me that both faster and more accurately: SSMS.

Now a much more useful question form, both in interviews and for SSC (IMHO), is "When I run this in SSMS, I get this. WHY?" You just have to make sure that there is a definitive and deterministic answer (which admittedly takes some work to get right).

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search