XML
XML

Value Method

Syntax
value(XQuery, SQLType)
XML

Value Method

Arguments

XQuery

Is the XQuery expression, a string literal, that retrieves data inside the XML instance. The XQuery must return at most one value. Otherwise, an error is returned

SQLType

Is the preferred SQL type, a string literal, to be returned. The return type of this method matches the SQLType parameter. SQLType can be an, image, text, ntext, nvarchar, varchar

XML

Value Method

Examples

DECLARE @doc XML = '
	<Product>  
		<Description id="1" name="Road Bike">  
			<Features>  
  				<Warranty>1 year</Warranty>  
  				<Maintenance>3 year</Maintenance>  
			</Features>  
		</Description>  
	</Product>';
SELECT @doc.value('(Product/Decription/@id)[1]', 'int');
XML

Query Method

Syntax
query('XQuery')
Arguments

Xquery is a string, an XQuery expression, that queries for XML nodes such as elements, in an XML instance

XML

Query Method

Example
SELECT @doc.query('//Product/Descrition/Features');
XML

Exist Method

Syntax
exist('XQuery')
Arguments

XQuery Is an XQuery expression, a string literal.

XML

Exist Method

Example
@doc.query('//Product[@id="1"]');
XML

Modify Method

Insert Element
@doc.modify('insert <Color name="red"></Color> 
	into (/Product/Description/Features)[1]')
XML

Insert Attribute

@doc.mofify('insert attribute color{"red"} 
	into (/Product/Description)[1]')
XML

Insert Attribute

DECLARE @color VARCHAR(4) = "red";
@doc.mofify('insert attribute color{sql:variable("@color")} 
	into (/Product/Description)[1]')
XML

Update Attribute

@doc.mofify('replace value of (/Product/Descrition/@color)[1] 
	with "blue"')
XML

Update Attribute

DECLARE @color = "blue";
@doc.mofify('replace value of (/Product/Descrition/@color)[1] 
	with sql:variable("@color")')
XML

Update Text

@doc.mofify('replace value of 
	(/Product/Descrition/Features/Warranty[1]/text())[1] 
	with 2 years')
XML

Delete Text

@doc.mofify('delete /Product/Description/Features/text()')
XML

Delete Attribute

@doc.mofify('delete /Product/Description/@color')