Solved

Simple example of Xquery select doesn't work

Posted on 2010-11-11
2
291 Views
Last Modified: 2012-06-22
Good Afternoon Experts,

I'm using the xml data type for the first time , and I don't see where is the error in the following example:
-------------
declare @myDoc xml
declare @ProdID varchar(20)
set @myDoc = '<processRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" source="RequestBusinessServiceBooking" destination="" propertyKey="10157" name="" GUID="5-5" vendorID="eS">
  <input xmlns="http://www.yourdomain.com">
<Bookings>
      <Booking>
        <BookingDetails key="96363.10149" name="" secondaryName="" abbreviation="" bookingTypeKey="2.10149.2" bookingType="GRP" arriveDate="2010-11-05" departDate="2010-11-05" status="prospect" decisonDate="2010-10-30" cutoffDate="2010-10-16" blockCode="" compRooms="0" commissionPer="0" statusDate="2010-10-16" createDate="2010-10-16" pickupComplete="false">
          <BookedBy key="280547.10149" name="Jessica Alba" initials="JFO" email="jalba@yourdomain.com" />
          <MarketSegment key="2.10149" name="Social" abrv="SOCL" />
        </BookingDetails>
      </Booking>
    </Bookings>
  </input>
</processRequest>'

set @ProdID =  @myDoc.value('(/processRequest/input/Bookings/Booking/BookingDetails/@bookingType)[1]', 'varchar(20)' )
select @ProdID
---------------------

the returned value is NULL

Thanks in advance for your help
0
Comment
Question by:cpertuz
2 Comments
 

Accepted Solution

by:
cpertuz earned 0 total points
ID: 34116767
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34116798
Your xml is not suitable at all for lesson #1!!
It includes namespaces which can be tricky
declare @myDoc xml
declare @ProdID varchar(20)
set @myDoc = '<processRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" source="RequestBusinessServiceBooking" destination="" propertyKey="10157" name="" GUID="5-5" vendorID="eS">
  <input xmlns="http://www.yourdomain.com">
<Bookings>
      <Booking>
        <BookingDetails key="96363.10149" name="" secondaryName="" abbreviation="" bookingTypeKey="2.10149.2" bookingType="GRP" arriveDate="2010-11-05" departDate="2010-11-05" status="prospect" decisonDate="2010-10-30" cutoffDate="2010-10-16" blockCode="" compRooms="0" commissionPer="0" statusDate="2010-10-16" createDate="2010-10-16" pickupComplete="false">
          <BookedBy key="280547.10149" name="Jessica Alba" initials="JFO" email="jalba@yourdomain.com" />
          <MarketSegment key="2.10149" name="Social" abrv="SOCL" />
        </BookingDetails>
      </Booking>
    </Bookings>
  </input>
</processRequest>'

;with XMLNAMESPACES ('http://www.yourdomain.com' as i)
select @ProdID =  @myDoc.value('(/processRequest/i:input/i:Bookings/i:Booking/i:BookingDetails/@bookingType)[1]', 'varchar(20)' )
select @ProdID

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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