我正在使用此查询:
select
SalesOrderID, OrderDate, DueDate, rowguid
from
Sales.SalesOrderHeader
where
SalesOrderID between 43661 and 43662
for xml path('Order'), Root('AllOrders')
这个输出是:
<AllOrders>
<Order>
<SalesOrderID>43661</SalesOrderID>
<OrderDate>2005-07-01T00:00:00</OrderDate>
<DueDate>2005-07-13T00:00:00</DueDate>
<rowguid>D91B9131-18A4-4A11-BC3A-90B6F53E9D74</rowguid>
</Order>
<Order>
<SalesOrderID>43662</SalesOrderID>
<OrderDate>2005-07-01T00:00:00</OrderDate>
<DueDate>2005-07-13T00:00:00</DueDate>
<rowguid>4A1ECFC0-CC3A-4740-B028-1C50BB48711C</rowguid>
</Order>
</AllOrders>
我希望这是预期的输出(xmlns:xsd
添加):
<AllOrders xmlns:xsd="some schema URL">
<Order>
<SalesOrderID>43661</SalesOrderID>
<OrderDate>2005-07-01T00:00:00</OrderDate>
<DueDate>2005-07-13T00:00:00</DueDate>
<rowguid>D91B9131-18A4-4A11-BC3A-90B6F53E9D74</rowguid>
</Order>
<Order>
<SalesOrderID>43662</SalesOrderID>
<OrderDate>2005-07-01T00:00:00</OrderDate>
<DueDate>2005-07-13T00:00:00</DueDate>
<rowguid>4A1ECFC0-CC3A-4740-B028-1C50BB48711C</rowguid>
</Order>
</AllOrders>
我要对查询进行哪些修改才能实现上述输出?
答案 0 :(得分:3)
您可以使用XMLNAMESPACES
;
WITH XMLNAMESPACES ('http://foo.com/bar' AS xsd)
select SalesOrderID,OrderDate,DueDate...
答案 1 :(得分:2)
通常,您始终可以转换为nvarchar(max)并进行文本替换。
这意味着你可以这样做
select replace(cast((
select SalesOrderID,OrderDate,DueDate,rowguid from Sales.SalesOrderHeader
where SalesOrderID between 43661 and 43662
for xml path('Order'), Root('AllOrders')
) as nvarchar(max)), N'<AllOrders>', N'<AllOrders xmlns:xsd="http://foo.com/bar">')
虽然我更希望你按照@AlexK
的建议去做