sql 用于XML示例的MS SQL
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 用于XML示例的MS SQL相关的知识,希望对你有一定的参考价值。
SELECT Top 2 Cust.CustomerID,
OrderHeader.SalesOrderID,
OrderHeader.Status
FROM Sales.Customer Cust
INNER JOIN Sales.SalesOrderHeader OrderHeader
ON Cust.CustomerID = OrderHeader.CustomerID
FOR XML Auto | Raw | Path | ;
-- ******** AUTO ********
-- <Cust CustomerID="29825">
-- <OrderHeader SalesOrderID="43659" Status="5" />
-- </Cust>
-- <Cust CustomerID="29672">
-- <OrderHeader SalesOrderID="43660" Status="5" />
-- </Cust>
-- ******** RAW ********
-- <row CustomerID="29825" SalesOrderID="43659" Status="5" />
-- <row CustomerID="29672" SalesOrderID="43660" Status="5" />
-- ******** PATH ********
-- <row>
-- <CustomerID>29912</CustomerID>
-- <SalesOrderID>43684</SalesOrderID>
-- <Status>5</Status>
-- </row>
-- <row>
-- <CustomerID>29849</CustomerID>
-- <SalesOrderID>43696</SalesOrderID>
-- <Status>5</Status>
-- </row>
-- ******** EXPLICIT ********
SELECT TOP 2 1 AS Tag,
NULL AS Parent,
Cust.CustomerID AS [Cust!1!ID],
OrderHeader.SalesOrderID AS [Cust!1!SOrdID],
OrderHeader.Status AS [Cust!1!Status]
FROM Sales.Customer Cust
INNER JOIN Sales.SalesOrderHeader OrderHeader
ON Cust.CustomerID = OrderHeader.CustomerID
FOR XML EXPLICIT;
-- <Cust ID="29825" SOrdID="43659" Status="5" />
-- <Cust ID="29672" SOrdID="43660" Status="5" />
-- Other Styles
-- Style 1
-- https://stackoverflow.com/a/5658758
SELECT CAST(
(SELECT TOP 2 C.CustomerID, C.AccountNumber
FROM Sales.Customer C
FOR XML PATH('')
)
AS VARCHAR(MAX)
) AS XMLDATA ;
-- <CustomerID>1</CustomerID>
-- <AccountNumber>AW00000001</AccountNumber>
-- <CustomerID>2</CustomerID>
-- <AccountNumber>AW00000002</AccountNumber>
-- Style 2
SELECT TOP 2 C.CustomerID, C.AccountNumber
FROM Sales.Customer C
FOR XML AUTO,
TYPE,
XMLSCHEMA,
ELEMENTS XSINIL;
以上是关于sql 用于XML示例的MS SQL的主要内容,如果未能解决你的问题,请参考以下文章
sql MS SQL for XML语法
sql MS SQL Xml检查是否存在
sql MS SQL特定格式到XML
sql MS SQL Parse XML格式2
sql MS SQL Parse XML格式1
sql MS SQL事务示例2