Answered XML string to return Table Rows

  • Friday, August 03, 2012 2:36 PM
     
      Has Code

    hi experts I have got a XML string. which i want to pass to sql to get Table Rows /Column out of it,

    can some one show me how can i do this.

    here is my xml 

    DECLARE @xml xml
    SET @xml= '<Receiptsui>
      <ui>3501d989-308c-481c-8b2a-f92230d3f9bc</ui>
      <ui>da832de7-b719-43bb-bfd6-e386eb6dbbda</ui>
      <ui>11c26563-acb0-4aac-aabd-ef12022feae6</ui>
      <ui>2e200248-8755-41e9-8be2-922422110443</ui>
      <ui>e77dffa1-5be9-4671-a819-9ec77060b993</ui>
      <ui>c507433b-2d05-4f8d-beb3-12bcc5fc66b2</ui>
    </Receiptsui>'
     
    SELECT
    	T.n.value('@ui[1]', 'uniqueidentifier')  [ui]
    FROM
    	@xml.nodes('/Receiptsui/ui') T(n)
    GO

    i tried above code, but it is giving null rows.

All Replies

  • Friday, August 03, 2012 2:48 PM
     
     Answered Has Code

    from the link

    http://social.msdn.microsoft.com/Forums/en-US/sqlxml/thread/359d2a08-9af3-44c6-976e-38bfb642974e

    Mr.

    DECLARE @xml xml
    SET @xml= '<Receiptsui>
      <ui>3501d989-308c-481c-8b2a-f92230d3f9bc</ui>
      <ui>da832de7-b719-43bb-bfd6-e386eb6dbbda</ui>
      <ui>11c26563-acb0-4aac-aabd-ef12022feae6</ui>
      <ui>2e200248-8755-41e9-8be2-922422110443</ui>
      <ui>e77dffa1-5be9-4671-a819-9ec77060b993</ui>
      <ui>c507433b-2d05-4f8d-beb3-12bcc5fc66b2</ui>
    </Receiptsui>'
     
    SELECT
    	T.n.value('.', 'uniqueidentifier')  [ui]
    FROM
    	@xml.nodes('/Receiptsui/ui') T(n)

    Wbob Has given the solution.

    replacing   '@ui[1]'  with '.' after T.n.values i got the result.

    thanks to forum experts. they spend so much of time and effort to help people like me.

    • Marked As Answer by Sushil Agarwal Friday, August 03, 2012 2:48 PM
    •