tailieunhanh - Using the SQL Server FOR XML Clause

Sử dụng SQL Server FOR XML khoản Với một tuyên bố tiêu chuẩn SELECT SQL, bạn gửi câu lệnh SELECT của bạn vào cơ sở dữ liệu để thực hiện và nhận được kết quả trở lại dưới hình thức hàng. SQL Server mở rộng các câu lệnh SELECT để cho phép bạn truy vấn cơ sở dữ liệu và nhận được kết quả lại như XML. Để làm điều này, bạn thêm một FOR XML khoản vào cuối câu lệnh SELECT của bạn. . | Using the SQL Server FOR XML Clause With a standard SQL SELECT statement you submit your SELECT statement to the database for execution and get results back in the form of rows. SQL Server extends the SELECT statement to allow you to query the database and get results back as XML. To do this you add a FOR XML clause to the end of your SELECT statement. The FOR XML clause specifies that SQL Server is to return results as XML. The FOR XML clause has the following syntax FOR XML RAW AUTO EXPLICIT XMLDATA ELEMENTS BINARY BASE64 The RAW AUTO and EXPLICIT keywords indicate the XML mode. Table shows a description of the keywords used in the FOR XML clause. In the next sections you ll examine some examples of the use of the FOR XML clause. Table FOR XML KEYWORDS KEYWORD DESCRIPTION RAW Specifies that each row in your result set is returned as an XML row element. The column values for each row in the result set become attributes of the row element. AUTO Specifies that each row in the result set is returned as an XML element The name of the table is used as the name of the tag in the row elements. EXPLICIT Indicates your SELECT statement specifies a parent-child relationship. This relationship is then used by SQL Server to generate XML with the appropriate nested hierarchy. XMLDATA Specifies that the XML schema is to be included in the returned XML. ELEMENTS Specifies that the column values are returned as subelements of the row otherwise the columns are returned as attributes of the row. You can use this option only with the AUTO mode. BINARY BASE64 Specifies that any binary data returned by your SELECT statement is encoded in base 64. If you want to retrieve binary data using either the RAW or EXPLICIT mode then you must use the BINARY BASE64 option. Using the RAW Mode You use the RAW mode to specify that each row in the result set returned by your SELECT statement is returned as an XML row element. The column values for each row in the result set become .