Thursday, March 22, 2012

Error in X-Path Query

I am new to XML in SQL Server and X-Path Query

Recently I have done the following virtual lab in techNet site

SQL Server 2005 XML Capabilities

The following query is worked fine in the lab

SELECTTOP 10 Demographics.query('declare default element namespace=

"http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey"

/IndividualSurvey/YearlyIncome')

FROM Sales.Individual

But later I installed SQL Server Express in my system and also the Express Manager CTP

but i am getting the following error if I execute the above query in my system

Msg 9317, Level 16, State 1, Line 1

XQuery [Sales.Individual.Demographics.query()]: Syntax error near '=', expected string literal.

Check ou this link: http://tinyurl.com/avqxx

Check out this syntax. I think your syntax is wrong and are missing a ref to the namespace and a ' ; '.

I.e

SELECT MyXml.query('

declare namespace s="http://myns/mydemoschema";

/s:root/s:product[@.s:id="304"]/s:name')FROM MyTable

notice the ; indeclare namespace s="http://myns/mydemoschema";

|||i think the name space is required when we are assigning the declared name space to a reference such as "s=" in the above example|||take default element out too.|||

Finally It is solved

I found the problem from another forum athttp://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=145&messageid=248323

Here is the answer given by Veteran

To declare default namespace, there is no "=" after namespace keyword. And there is a ";" after the namespace string. The following query works:

SELECTTOP 10 Demographics.query('declare default element namespace "http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey";

/IndividualSurvey/YearlyIncome')

FROM Sales.Individual

No comments:

Post a Comment