+ Reply to Thread
Results 1 to 3 of 3

Thread: Select from XML datatype column

  1. #1
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    1,040
    Blog Entries
    2
    Rep Power
    13

    Select from XML datatype column

    I have a hard nut to crack here. I have the following XML stored in an XML field in a table -- using SQL Server 2005:
    Code:
    <root>
      <relationship table="Bob" source="Z">
         <key column="Col1" />
         <key column="Col2" />
     </relationship>
     <relationship table="Ted" source="Y">
        <key column="Col3"/>
     </relationship>
    </root>
    
    Now, I'm needing a SELECT query that will return this in the following format:
    Code:
    Bob   Z  Col1
    Bob   Z  Col2
    Ted   Y  Col3
    
    All attempts have been futile. Any T-SQL, XQuery-ers out there?

    And yes, there are some good reasons why I'm storing data this way
    Wolffy
    ------------------------
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,354
    Blog Entries
    9
    Rep Power
    19

    Well, this article seems to explain the basics...but not knowing what you've tried, I'm not sure how much help it would be: 15 Seconds : SQL Server 2005 XQuery and XML-DML - Part 1
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  3. #3
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    1,040
    Blog Entries
    2
    Rep Power
    13

    I love this site!!!! Every time I post a problem I've been noodling on all day -- I figure it out 30 minutes later. W00t!

    Anyway -- the query is:
    Code:
    Select 
           T.c.value('@table', 'nvarchar(128)')  
        ,  T.c.value('@source', 'nvarchar(128)')    
        ,  S.c.value('@column', 'nvarchar(128)')  
    From myTable
    Cross Apply xmlField.nodes('/root/relationship') as T(c)
    Cross Apply T.c.nodes('./key') as S(c)
    
    Wolffy
    ------------------------
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Rework for your specific environment may be required. Void where prohibited by law. Not valid in California. Your mileage may vary.

+ Reply to Thread

Similar Threads

  1. money datatype problem
    By todd2006 in forum Microsoft SQL Server
    Replies: 2
    Last Post: February 13th, 2009, 05:06 PM
  2. Show image according to column width
    By micky in forum .Net Development
    Replies: 14
    Last Post: December 12th, 2008, 05:30 AM
  3. use text box instead of table column
    By guddu in forum ASP Development
    Replies: 5
    Last Post: November 18th, 2008, 08:06 AM
  4. Ambiguous column error
    By Rebelle in forum Microsoft SQL Server
    Replies: 1
    Last Post: November 5th, 2008, 11:37 AM
  5. Dynamic dropdown not fit in column
    By guddu in forum ASP Development
    Replies: 6
    Last Post: August 19th, 2008, 12:27 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO