[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Simple example of Xquery select doesn't work

Posted on 2010-11-11
2
Medium Priority
?
337 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

825 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