0

I'm trying to use XQuery on text data that I've cast to XML, without success. So this fails:

    DECLARE @myDoc TEXT  
    SET @myDoc = '<Root>  
    <ProductDescription ProductID="1" ProductName="Road Bike">  
    <Features>  
      <Warranty>1 year parts and labor</Warranty>  
      <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>  
    </Features>  
    </ProductDescription>  
    </Root>'  

SELECT CAST(@myDoc AS XML)('(/Root/ProductDescription/@ProductID)[1]', 'int' )  

but this succeeds:

DECLARE @myDoc xml  
DECLARE @ProdID int  
SET @myDoc = '<Root>  
<ProductDescription ProductID="1" ProductName="Road Bike">  
<Features>  
  <Warranty>1 year parts and labor</Warranty>  
  <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>  
</Features>  
</ProductDescription>  
</Root>'  

SELECT  @myDoc.value('(/Root/ProductDescription/@ProductID)[1]', 'int' )  
Eric
  • 2,861
  • 6
  • 28
  • 59

1 Answers1

0

I figured out what David Brown was talking about, this works:

DECLARE @myDoc NVARCHAR(MAX)  
DECLARE @ProdID int  
SET @myDoc = '<Root>  
<ProductDescription ProductID="1" ProductName="Road Bike">  
<Features>  
  <Warranty>1 year parts and labor</Warranty>  
  <Maintenance>3 year parts and labor extended maintenance is available</Maintenance>  
</Features>  
</ProductDescription>  
</Root>'  

SELECT CAST(@myDoc AS XML).value('(/Root/ProductDescription/@ProductID)[1]', 'int' )    
Eric
  • 2,861
  • 6
  • 28
  • 59