• Aucun résultat trouvé

replace value of

Dans le document SQL Server (Page 128-133)

The updatekeyword in conjunction with the replace value ofkeyword allows for the in-place update of a node value in an XML instance. Chapter 2 covered the syntax of the replace value of keyword, but it is shown again here for review and additional information:

replace value of expression1 with

expression2

Expression1is the node whose value is being updated. Only a single node can be expressed; if multi-ple nodes are expressed, an error is generated.

Expression2is the new value of the node.

The following example updates a node in an XML instance with a new value. Using the previous Employee example, the JobTitlevalue is updated with a new value as follows:

DECLARE @xmldoc xml SET @xmldoc =

‘<Root>

<Employee EmployeeID=”1”>

<EmployeeInformation>

<FirstName>Evel</FirstName>

<LastName>Knievel</LastName>

<JobTitle>Daredevil</JobTitle>

</EmployeeInformation>

</Employee>

</Root>’

-- update text in the first manufacturing step SET @xmldoc.modify(‘

replace value of (/Root/Employee/EmployeeInformation/JobTitle[1]/text())[1]

with “Retired”

‘)

SELECT @xmldoc

The results of the SELECTstatement (see Figure 5-17) show that the JobTitlevalue has been changed from Daredevilto Retired:

Figure 5-17

Attributes can also be updated as shown in the following example (also note the change in information in the XML instance):

DECLARE @xmldoc xml SET @xmldoc =

‘<Root>

<Employee EmployeeID=”1”>

<EmployeeInformation>

<FirstName>Robby</FirstName>

<LastName>Knievel</LastName>

<JobTitle>Son</JobTitle>

</EmployeeInformation>

</Employee>

</Root>’

-- update text in the first manufacturing step SET @xmldoc.modify(‘

replace value of (/Root/Employee/EmployeeInformation/JobTitle[1]/text())[1]

with “Daredevil”

‘)

SET @xmldoc.modify(‘

replace value of (/Root/Employee/@EmployeeID)[1]

with “2”

‘)

SELECT @xmldoc

This example updates both the EmployeeIDattribute as well as the JobTitlevalue, as shown in Figure 5-18.

In the previous examples, a [1]is added to the end of the target value being updated. Since only a sin-gle node can be updated, the [1]value specifies which node to update. In these examples, the [1]is not really necessary because there is only one <JobTitle>node.

Figure 5-18

However, in the case of multiple nodes with the same name, such as the case with the Motocross exam-ples where there are multiple <Rider>nodes, the [1]is necessary. A value of [1]updates the first

<Rider>node, while a [2]updates the second <Rider>node.

For example, the following code updates the second rider <Rider>node of the third Teamnode of the Motocross table (the column is untyped):

UPDATE Motocross

SET MotocrossInfo.modify (‘

replace value of (/Motocross/Team/Rider/text())[2]

with “Davi Millsaps” ‘)

This example demonstrates the replace value ofstatement, which updates the name of the second rider for team Suzuki from Broc Heplerto Davi Millsaps. The modified results are shown here:

<Rider BikeSize=”250”>Jeremy McGrath</Rider>

</Team>

<Team Manufacturer=”Suzuki”>

<Rider BikeSize=”250”>Ricky Carmichael</Rider>

<Rider BikeSize=”125”>Davi Millsaps</Rider>

<Rider BikeSize=”250”>Sebastien Tortelli</Rider>

</Team>

<Team Manufacturer=”Kawasaki”>

<Rider BikeSize=”250”>James Stewart</Rider>

<Rider BikeSize=”125”>Michael Byrne</Rider>

The first expression identifies the node whose value is to be replaced and must be a single node. An error is generated if multiple nodes are found in the results of the query. Equally, if the results of the first expression are empty, no replacement is made.

The second expression specifies the new value of the node — either a single value or a list of values. In the case where it is a list of values, the old value is replaced with the list.

The last example in this section uses conditional statements to determine the new value. In the following example, the expression queries the number of riders for the first team, and depending on the number of riders found, sets the attribute of the Teamelement to a different value:

DECLARE @xmldoc xml SET @xmldoc = ‘<Motocross>

<Team Manufacturer=”Yamaha”>

<Rider BikeSize=”250”>Tim Ferry</Rider>

<Rider BikeSize=”250”>Chad Reed</Rider>

<Rider BikeSize=”250”>David Vuillemin</Rider>

</Team>

<Team Manufacturer=”Honda”>

<Rider BikeSize=”450”>Kevin Windham</Rider>

<Rider BikeSize=”250”>Mike LaRacco</Rider>

<Rider BikeSize=”250”>Jeremy McGrath</Rider>

</Team>

<Team Manufacturer=”Suzuki”>

<Rider BikeSize=”250”>Ricky Carmichael</Rider>

<Rider BikeSize=”125”>Broc Hepler</Rider>

<Rider>Sebastien Tortelli</Rider>

</Team>

<Team Manufacturer=”Kawasaki”>

<Rider BikeSize=”250”>James Stewart</Rider>

<Rider BikeSize=”125”>Michael Byrne</Rider>

</Team>

</Motocross>’

SET @xmldoc.modify(‘

replace value of (/Motocross/Team[1]/@Manufacturer)[1]

with (

if (count(/Motocross/Team[1]/Rider) = 3) then

“Team Yamaha”

else

“Yamaha”

)

‘)

SELECT @xmldoc

The results shown in Figure 5-19 illustrate that the attribute on the Teamelement for the Yamaha manu-facturer has changed for the Yamaha Team.

Figure 5-19

This example used the count()function to count the number of child nodes. While this example is fairly simplistic, conditional expressions used in XQuery expressions have to use every XQuery function available at its disposal.

For example, an expression could check the value of a node and the conditional expression could base its decision on the return of that value.

Summar y

The entire purpose of this chapter was to build on the related topics that were discussed in Chapter 2.

The implementation of XQuery support in SQL Server 2005, with the addition of the XML DML, makes the querying and modification of the xmldata type quite easy.

The XQuery language is quickly becoming a very popular and common XML querying language, and it would be wise to start learning it. This chapter got you started with that endeavor by explaining the syn-tax and structure of XQuery and some discussions of the concepts and terms used with XQuery, such as sequences and atomization.

That was followed by an in-depth discussion on the XML DML (Data Modification Language), which is an extension of the XQuery language and used to modify the data within an XML instance. You learned about the three case-sensitive keywords (insert, delete, and replace value of), which allow you to modify XML document content.

From here it is time to learn and understand how to improve performance when querying XML docu-ments by learning about indexing the xmldata type.

Indexing XML Data in

Dans le document SQL Server (Page 128-133)