Apr 27, 2014

XQuery [query()]: The name "" does not denote a namespace

If we will execute following XQuery in sql server:

DECLARE @s AS XML = '
      <P:Root xmlns:P="http://www.exacthelp.com">
            <P:Student Name="Scott"/>
            <P:Stuent Name="Greg"/>
      </P:Root>'
SELECT @s.query('P:Root/P:Student')

We may get error message something like this:

Msg 2229, Level 16, State 1, Line 6
XQuery [query()]: The name "P" does not denote a namespace.

Cause: It is incorrect to use namespace in Xquery in this way.

Solution:

I'm telling four solutions. Correct  Follow any one solution which you want:

1.
SELECT @s.query
      ('declare default element namespace "http://www.exacthelp.com";
       /Root/Student')

2.
      
SELECT @s.query
      ('declare namespace P = "http://www.exacthelp.com";
       /P:Root/P:Student')

3.

;WITH XMLNAMESPACES(DEFAULT 'http://www.exacthelp.com')    
SELECT @s.query('/Root/Student')

4.

;WITH XMLNAMESPACES('http://www.exacthelp.com' AS P) 

SELECT @s.query('/P:Root/P:Student')     

No comments:

Post a Comment