Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

XML Extract

$
0
0

DECLARE @xmlDoc XML;
DECLARE @handle INT;

DECLARE @Xml XML = N'
<propertyList date="2013-05-21-09:13:38">
<residential modTime="2013-05-21-09:13:39" status="sold">
        <agentID>a1fa</agentID>
        <uniqueID>2152459</uniqueID>
         <buildingDetails>

- <listingAgent id="1">
  <name>David </name>
  <telephone type="BH">49666</telephone>
  <telephone type="mobile">238394</telephone>
  <email>david@gmail.com</email>
  </listingAgent>
- <listingAgent id="2">
  <name>Bryan</name>
  <telephone type="BH"> 499 1666</telephone>
  <telephone type="mobile">344 2630</telephone>
  <email>bryan@gmail.com</email>
  </listingAgent>

  </buildingDetails>
 </residential>
</propertyList>
';



EXEC SP_XML_PREPAREDOCUMENT @handle OUTPUT,@xmlDoc



SELECT     DISTINCT 'Commercial' PropertyType,residential.query('agentID').value('.', 'VARCHAR(100)') AgentID,
        residential.query('uniqueID').value('.', 'VARCHAR(100)') PropertyID,
        residential.value('listingAgent[1]/@id', 'VARCHAR(100)') id,
         residential.value('listingAgent[1]/name[1]', 'VARCHAR(100)')  Agent_2_Name ,
        residential.value('listingAgent[1]/email[1]', 'VARCHAR(100)') Agent_2_Email,
        telephone.value('telephone[1]/@type', 'VARCHAR(100)') Agent_2_PhoneType,
      residential.value('listingAgent[1]/telephone[1]', 'VARCHAR(100)') Agent_2_Phone#



FROM    @xmlDoc.nodes('/propertyList/commercial') AS propertyList (residential)
  CROSS APPLY ( SELECT    telephone.query('.')
                      FROM      residential.nodes('listingAgent[1]/telephone') residential ( telephone )
                    ) listingAgent ( telephone )

UNION ALL

SELECT    distinct 'Commercial',residential.query('agentID').value('.', 'VARCHAR(100)') AgentID,
        residential.query('uniqueID').value('.', 'VARCHAR(100)') PropertyID,
        residential.value('listingAgent[2]/@id', 'VARCHAR(100)') id,
         residential.value('listingAgent[2]/name[1]', 'VARCHAR(100)')  Agent_2_Name ,
        residential.value('listingAgent[2]/email[1]', 'VARCHAR(100)') Agent_2_Email,
        telephone.value('  telephone[2]/@type', 'VARCHAR(100)') Agent_2_PhoneType,
      residential.value('listingAgent[2]/telephone[2]', 'VARCHAR(100)') Agent_2_Phone#

FROM    @xmlDoc.nodes('/propertyList/commercial') AS propertyList (residential)
  CROSS APPLY ( SELECT    telephone.query('.')
                      FROM      residential.nodes('listingAgent[2]/telephone') residential ( telephone )
                    ) listingAgent ( telephone )

I want to get name,phone type,phone number and email id

i am trying to get ListingAgent 1 and ListingAgent2 information,  getting no rows,

is there anything missing in my query

Thanks

Kalyan

                          

Viewing all articles
Browse latest Browse all 23857

Trending Articles