for xml clause

Specifies a SQL select statement that returns an XML representation of the result set.

Syntax 

 select ::=
     select [ all | distinct ] select_list
     [into_clause ]
     [where_clause ]
     [group_by_clause ]
     [having_clause ]
     [order_by_clause ]
     [compute_clause ]
     [read_only_clause ]
     [isolation_clause ]
     [browse_clause ]
     [plan_clause]
     [for_xml_clause]
 for_xml_clause ::=
     for xml [schema | all] [option option_string] [returns_clause]
 option_string ::= basic_string_expression
 returns_clause ::=
     returns { char [(integer)] | varchar [(integer)] 
     |unichar [(integer)] | univarchar [(integer)]
     |text | unitext | java.lang.String}

NoteSee “option_strings: general format” for more information about option strings.

NoteSee Chapter 6, “XML Support for I18N” for more information on using for xml with I18N data.

Description

For example, if you execute this query in Adaptive Server 12.5.2, the results are:

select getdate() for xml 
<resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
< row > 
< C1 > 2008-05-30 11:42:19 < /C1 > 
< /row > 
< /resultset > 

But in Adaptive Server 15.0.2, the results from the same query are:

select getdate() for xml 
<resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
< row > 
< C1 > 2008-05-30T11:41:42 < /C1 > 
< /row > 
< /resultset > 

Options

The general format of the option_string is specified in “option_strings: general format”. The options for the for xml clause are specified in “SQLX options”.

Exceptions

Any SQL exception raised during execution of the basic select statement is raised by the for xml l select. For example, both of the following statements raise a zero divide exception:

select 1/0
select 1/0 for xml

Example

The for xml clause:

select pub_id, pub_name 
from pubs2.dbo.publishers
for xml
----------------
<resultset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
   <pub_id>0736</pub_id>
   <pub_name>NewAgeBooks</pub_name>
</row>

<row>
   <pub_id>0877</pub_id>
   <pub_name>Binnet & Hardley</pub_name>
</row>

<row>
   <pub_id>1389</pub_id>
   <pub_name>Algodata Infosystems</pub_name>
</row>

</resultset>