• Aucun résultat trouvé

The TYPE Directive

Dans le document SQL Server (Page 170-173)

With the addition of the xmldata type in SQL Server 2005, it only makes sense to make that data type available everywhere XML is concerned. Why should FOR XMLbe any different? To this end, SQL Server 2005 provides the capability to specify that the results of a FOR XMLstatement be returned as an xml data type. This is accomplished by adding the TYPEdirective to the FOR XMLstatement at the end of the SELECTstatement.

If FOR XMLis specified in a SELECTstatement without the TYPEdirective, the results are returned as an XML instance in a nvarchar(max)data type.

The general syntax of the FOR XMLstatement using the TYPEdirective is as follows:

SELECT Columnname(s) FROM Tablename FOR XML, TYPE

The easiest way to demonstrate this is with examples. The first example selects a couple of columns from the Person.Contact table and returns them as an xmldata type using the TYPEdirective:

SELECT HRE.EmployeeID, PC.FirstName, PC.LastName, HRE.Title FROM Person.Contact PC, HumanResources.Employee HRE

WHERE PC.ContactID = HRE.ContactID ORDER BY HRE.EmployeeID

FOR XML RAW, TYPE

The results of this query are shown in Figure 8-1.

Figure 8-1

Take off the TYPEdirective and rerun the query. Notice that the results look exactly the same. The only difference is that by specifying the TYPEdirective, the results were returned as an xmldata type whereas by taking off the TYPEdirective the results were returned as a nvarchar(max)data type.

This example used the RAWmode, which returned the results as single <row>elements.

Applying some of the knowledge learned from previous chapters in the book, this next example uses the query()method of the xmldata type to query the results returned by the FOR XMLquery:

SELECT (SELECT FirstName, LastName, EmailAddress, AdditionalContactInfo.query(‘

declare namespace pc=”http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes”;

//pc:eMail/pc:eMailAddress

‘) as EmailAddress FROM Person.Contact

FOR XML AUTO, TYPE).query(‘/Person.Contact’) The results are shown in Figure 8-2.

Figure 8-2

In the results, notice that the FirstName, LastName, and EmailAddressvalues are returned as attributes. Had the ELEMENTSdirective been specified on the FOR XMLclause, these values would have been returned as elements and the results would look quite different.

The querymethod of the xmldata type is used to return a fourth value, the alternative e-mail address, which as you notice is returned as a separate element because of the As EmailAddress. The addition of the TYPEdirective returns these results as an xmldata type.

This next example shows the FOR XMLstatement and TYPEdirective being used with the xmldata type query()method, but all the xmldata type methods are eligible for use with FOR XML.

FOR XMLcan also be applied to variables as shown in the following example. This example queries the Contacts table using FOR XML, and the results are placed in an xmldata type variable:

DECLARE @xmlvar xml

SET @xmlvar = (SELECT HRE.ContactID, PC.FirstName,

PC.LastName,

PC.AdditionalContactInfo.query (‘

declare namespace ct=”http://schemas.microsoft.com/sqlserver/2004/07/

adventure-works/ContactTypes”;

//ct:eMail/ct:eMailAddress

‘)

as AdditionaleMail

FROM Person.Contact PC, HumanResources.Employee HRE WHERE PC.ContactID = HRE.ContactID

ORDER BY HRE.ContactID FOR XML AUTO, TYPE) SELECT @xmlvar GO

The results are shown in Figure 8-3.

Figure 8-3

This example is very similar to the previous example. The FirstNameand LastNamevalues are still returned as attributes, as in the previous example. The difference here is that the results are returned as an xmldata variable.

As a final note on this topic, FOR XMLcan also be combined with XML DMLstatements to provide UPDATE, DELETE, or INSERTfunctionality. For example, the following code first creates the Employee table with two xmldata type columns, inserts an XML document into the first xmldata type column,

and then updates the second xmldata type column using a FOR XML SELECTstatement and the query()method of the xmldata type:

DROP TABLE Employee GO

CREATE TABLE Employee (EmployeeID int, EmployeeInfo xml, EmployeeInfo2 xml NULL) GO

INSERT INTO Employee (EmployeeID, EmployeeInfo) VALUES(1, ‘<root><EmployeeInfo><Employee

EmployeeID=”1”></Employee></EmployeeInfo></root>’) GO

SELECT * FROM Employee

UPDATE Employee SET EmployeeInfo2 = (SELECT EmployeeInfo.query(‘/root’) FROM Employee row FOR XML AUTO, TYPE)

GO

SELECT * FROM Employee

The results from the query are shown in Figure 8-4.

Figure 8-4

In this example, the querymethod of the xmldata type is used to query all the contents of the root node (which in this example is the entire XML document). Those results are then inserted into the EmployeeInfo2 column.

Take a look at the FROMclause for a moment, and also look at the EmployeeInfo2 column of the second set of results. Notice that the query specified its own root node called row. This means that the root node in the EmployeeInfo column is not the rootnode in the EmployeeInto2 column; the rownode is.

Dans le document SQL Server (Page 170-173)