The FOR XML Clause : For XML « XML « SQL Server / T-SQL Tutorial






SELECT <column list>
[FROM <source table(s)>]
[WHERE <restrictive condition>]
[GROUP BY <column name or expression using a column in the SELECT list>
[HAVING <restrictive condition based on the GROUP BY results>]
[ORDER BY <column list>]
[FOR XML {RAW|AUTO|EXPLICIT}[, XMLDATA][, ELEMENTS][, BINARY base64]]
[OPTION (<query hint>, [, .n])]

13> CREATE TABLE Customers (
14>     CustomerID nchar (5) NOT NULL ,
15>     CompanyName nvarchar (40) NOT NULL ,
16>     ContactName nvarchar (30) NULL ,
17>     ContactTitle nvarchar (30) NULL ,
18>     Address nvarchar (60) NULL ,
19>     City nvarchar (15) NULL ,
20>     Region nvarchar (15) NULL ,
21>     PostalCode nvarchar (10) NULL ,
22>     Country nvarchar (15) NULL ,
23>     Phone nvarchar (24) NULL ,
24>     Fax nvarchar (24) NULL
25> )
26> GO
1>
2> INSERT Customers VALUES('1','A','Maria',    'Sales',  'Str. 57', 'Berlin'    ,NULL,'12209', 'Germany','111-1111111','111-1111111')
3> INSERT Customers VALUES('2','M','Joe',      'Owner',  'Ave. 231','Vancouver' ,NULL,'05023', 'Mexico', '(222) 222-3332',NULL)
4> INSERT Customers VALUES('3','H','Thomas',   'Sales',  'Sq.  111','London'    ,NULL,'1D00P', 'UK',     '(444) 444-4444','(444) 444-4444')
5> INSERT Customers VALUES('4','B','Berg',     'Order',  'Blv    8','Toronto'   ,NULL,'00222', 'Sweden', '4444-55 55 65','5555-55 55 55')
6> INSERT Customers VALUES('5','S','Moos',     'Sales',  'Fort  57','New York'  ,NULL,'68306', 'Germany','6666-66666','6666-77777')
7> INSERT Customers VALUES('6','F','Cite',     'Manager','24      ','Dalles'    ,NULL,'67000', 'France', '88.60.15.31','88.60.15.32')
8> INSERT Customers VALUES('7','C','Sommer',   'Owner',  'Araq, 67','Paris'     ,NULL,'28023', 'Spain',  '(91) 555 22 82','(91) 555 91 99')
9> INSERT Customers VALUES('8','P','Leb',      'Owner',  '12      ','Beijing'   ,NULL,'13008', 'France', '91.24.45.40','91.24.45.41')
10> INSERT Customers VALUES('9','D','Elizabeth','Manager','23 Blvd.','Tsawassen','BC', 'T2F8M4','Canada', '(604) 555-4729','(604) 555-3745')
11> go

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
1>
2>    SELECT Customers.CustomerID,
3>      Customers.CompanyName
4>    FROM Customers
5>    FOR XML RAW
6> GO
XML_F52E2B61-18A1-11d1-B105-00805F49916B


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
0x440A43007500730074006F006D006500720049004400440B43006F006D00700061006E0079004E0061006D006500440372006F007700010302010E0A00310020002000200020000202110200410043010302010E0A0032002000200020002000020211
02004D0043010302010E0A00330020002000200020000202110200480043010302010E0A00340020002000200020000202110200420043010302010E0A00350020002000200020000202110200530043010302010E0A0036002000200020002000020211
0200460043010302010E0A00370020002000200020000202110200430043010302010E0A003800200020002000200002021102005000430103

(1 rows affected)
1>
2> drop table Customers;
3> GO
1>








24.2.For XML
24.2.1.FOR XML Syntax
24.2.2.The FOR XML Clause
24.2.3.Select for FOR XML AUTO
24.2.4.FOR XML AUTO