TYPE Directive in FOR XML Queries(FOR XML查询中的TYPE指令)
2016-02-19 08:44
525 查看
SQL Server support for the xml (Transact-SQL) enables you to optionally request that the result of a FOR XML query be returned
as xml data type by specifying the TYPE directive. This allows you to process the result of a FOR XML query on the server. For example, you can specify an XQuery against it, assign the result to an xml type variable, or write Nested
FOR XML queries.
The following examples illustrate the use of the TYPE directive with FOR XML queries.
The following query retrieves customer contact information from the Contacts table. Because the TYPE directive
is specified in FOR XML, the result is returned as xml type.
This is the partial result:
<Person.Person BusinessEntityID="1" FirstName="Ken" LastName="Sánchez"/>
<Person.Person BusinessEntityID="2" FirstName="Terri" LastName="Duffy"/>
...
In the following example, a FOR XML result is assigned to an xml type variable, @x. The query retrieves contact information, such as theBusinessEntityID, FirstName, LastName,
and additional telephone numbers, from the AdditionalContactInfo column of xml TYPE.
Because the FOR XML clause specifies TYPE directive, the XML is returned
as xml type and is assigned to a variable.
The FOR XML queries return XML. Therefore, you can apply xml type methods, such as query() and value(), to the XML result returned by FOR XML queries.
In the following query, the query() method of the xml data type is used to query the result of the FOR
XML query. For more information, seequery() Method (xml Data Type).
The inner SELECT … FOR XML query returns an xml type result to which the outer SELECT applies
the query() method to the xml type. Note the TYPE directive
specified.
This is the result:
<Person.Person BusinessEntityID="1" FirstName="Ken" LastName="Sánchez">
<PhoneNumbers>
<act:number xmlns:act="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes">111-111-1111</act:number>
<act:number xmlns:act="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes">112-111-1111</act:number>
</PhoneNumbers>
</Person.Person>
In the following query, the value() method of the xml data type is used to retrieve a value from the XML result returned by the SELECT…FOR
XML query. For more information, see value() Method (xml Data Type).
The XQuery path expression in the value() method retrieves the first telephone number of a customer contact whose BusinessEntityID is 1.
The following example demonstrates how FOR XML queries can be used in Data Manipulation Language (DML) statements. In the example, theFOR XML returns an instance of xml type.
The INSERT statement inserts this XML into a table.
as xml data type by specifying the TYPE directive. This allows you to process the result of a FOR XML query on the server. For example, you can specify an XQuery against it, assign the result to an xml type variable, or write Nested
FOR XML queries.
Note |
---|
SQL Server returns XML data type instance data to the client as a result of different server-constructs such as FOR XML queries that use the TYPE directive, or where the xml data type is used to return XML instance data values from SQL table columns and output parameters. In client application code, the ADO.NET provider requests this XML data type information to be sent in a binary encoding from the server. However, if you are using FOR XML without the TYPE directive, the XML data comes back as a string type. In any case, the client provider will always be able to handle either form of XML. Note that top-level FOR XML without the TYPE directive cannot be used with cursors. |
Examples
The following examples illustrate the use of the TYPE directive with FOR XML queries.
Retrieving FOR XML query results as xml type
The following query retrieves customer contact information from the Contacts table. Because the TYPE directiveis specified in FOR XML, the result is returned as xml type.
USE AdventureWorks2012; Go SELECT BusinessEntityID, FirstName, LastName FROM Person.Person ORDER BY BusinessEntityID FOR XML AUTO, TYPE;
This is the partial result:
<Person.Person BusinessEntityID="1" FirstName="Ken" LastName="Sánchez"/>
<Person.Person BusinessEntityID="2" FirstName="Terri" LastName="Duffy"/>
...
Assigning FOR XML query results to an xml type variable
In the following example, a FOR XML result is assigned to an xml type variable, @x. The query retrieves contact information, such as theBusinessEntityID, FirstName, LastName,and additional telephone numbers, from the AdditionalContactInfo column of xml TYPE.
Because the FOR XML clause specifies TYPE directive, the XML is returned
as xml type and is assigned to a variable.
USE AdventureWorks2012; GO DECLARE @x xml; SET @x = ( SELECT BusinessEntityID, FirstName, LastName, AdditionalContactInfo.query(' declare namespace aci="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactInfo"; declare namespace act="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes"; //act:telephoneNumber/act:number') as MorePhoneNumbers FROM Person.Person FOR XML AUTO, TYPE); SELECT @x; GO
Querying results of a FOR XML query
The FOR XML queries return XML. Therefore, you can apply xml type methods, such as query() and value(), to the XML result returned by FOR XML queries.In the following query, the query() method of the xml data type is used to query the result of the FOR
XML query. For more information, seequery() Method (xml Data Type).
USE AdventureWorks2012; GO SELECT (SELECT BusinessEntityID, FirstName, LastName, AdditionalContactInfo.query(' DECLARE namespace aci="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactInfo"; DECLARE namespace act="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes"; //act:telephoneNumber/act:number ') AS PhoneNumbers FROM Person.Person FOR XML AUTO, TYPE).query('/Person.Person[1]');
The inner SELECT … FOR XML query returns an xml type result to which the outer SELECT applies
the query() method to the xml type. Note the TYPE directive
specified.
This is the result:
<Person.Person BusinessEntityID="1" FirstName="Ken" LastName="Sánchez">
<PhoneNumbers>
<act:number xmlns:act="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes">111-111-1111</act:number>
<act:number xmlns:act="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes">112-111-1111</act:number>
</PhoneNumbers>
</Person.Person>
In the following query, the value() method of the xml data type is used to retrieve a value from the XML result returned by the SELECT…FOR
XML query. For more information, see value() Method (xml Data Type).
USE AdventureWorks2012; GO DECLARE @FirstPhoneFromAdditionalContactInfo varchar(40); SELECT @FirstPhoneFromAdditionalContactInfo = ( SELECT BusinessEntityID, FirstName, LastName, AdditionalContactInfo.query(' declare namespace aci="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactInfo"; declare namespace act="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes"; //act:telephoneNumber/act:number ') AS PhoneNumbers FROM Person.Person Contact FOR XML AUTO, TYPE).value(' declare namespace act="http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ContactTypes"; /Contact[@BusinessEntityID="1"][1]/PhoneNumbers[1]/act:number[1]', 'varchar(40)' ) SELECT @FirstPhoneFromAdditionalContactInfo;
The XQuery path expression in the value() method retrieves the first telephone number of a customer contact whose BusinessEntityID is 1.
Note |
---|
If the TYPE directive is not specified, the FOR XML query result is returned as type nvarchar(max). |
Using FOR XML query results in INSERT, UPDATE, and DELETE (Transact-SQL DML)
The following example demonstrates how FOR XML queries can be used in Data Manipulation Language (DML) statements. In the example, theFOR XML returns an instance of xml type.The INSERT statement inserts this XML into a table.
CREATE TABLE T1(intCol int, XmlCol xml); GO INSERT INTO T1 VALUES(1, '<Root><ProductDescription ProductModelID="1" /></Root>'); GO CREATE TABLE T2(XmlCol xml) GO INSERT INTO T2(XmlCol) SELECT (SELECT XmlCol.query('/Root') FROM T1 FOR XML AUTO,TYPE); GO
相关文章推荐
- SQL中的三值逻辑
- SQL Server 作业批量停止
- 结束SQL阻塞的进程
- 动态生成SQL Server视图作业
- SQL Server 语句操纵数据库
- SQL(结构化查询语句)
- oracle sql日期比较
- linux快速部署mysql服务器
- sql 存储过程分页
- 在WINXP系统上安装SQL Server企业版的方法
- 通过批处理调用SQL的方法(osql)
- SQL Server 存储过程的分页
- ASP程序与SQL存储过程结合使用详解
- SQL SERVER编写存储过程小工具
- 防御SQL注入攻击时需要注意的一个问题
- PostgreSQL教程(十九):SQL语言函数
- SQL Server复制需要有实际的服务器名称才能连接到服务器
- SQL Server 2000向SQL Server 2008 R2推送数据图文教程