gpt4 book ai didi

sql - Sql Server 中的 XML 专用

转载 作者:行者123 更新时间:2023-12-04 07:40:03 26 4
gpt4 key购买 nike

我们有一个简单的表,需要转换为 XML

 Declare @Person TABLE
( [BusinessEntityID] [int] NOT NULL,
[PersonType] [varchar](2) NULL,
[Title] [varchar](30) NULL,
[FirstName] [varchar](30) NULL,
[MiddleName] [varchar](30) NULL,
[LastName] [varchar](30) NULL)

Insert Into @Person Values(10001,'IN','Article','Carolyn','Jo','Alonso')

我们需要这个 xml 用于在 sql server 中使用 For XML 进行输出
<?xml-stylesheet type='text/xsl' href='result.xsl'?>
<documents>
<document>
<field name="BusinessEntityID">10001</field>
<field name="PersonType">IN</field>
<field name="Title">Article</field>
<field name="FirstName">Carolyn</field>
<field name="MiddleName">Jo</field>
<field name="LastName">Alonso</field>
</document>
</documents>

最佳答案

以下 XML PATH 查询应该可以帮助您完成大部分工作。您只需要添加样式表标题。

SELECT 
'BusinessEntityID' AS 'document/field/@name'
, BusinessEntityID AS 'document/field'
, '' AS 'document'
, 'documentType' AS 'document/field/@name'
, PersonType AS 'document/field'
, '' AS 'document'
, 'Title' AS 'document/field/@name'
, Title AS 'document/field'
, '' AS 'document'
, 'FirstName' AS 'document/field/@name'
, FirstName AS 'document/field'
, '' AS 'document'
, 'MiddleName' AS 'document/field/@name'
, MiddleName AS 'document/field'
, '' AS 'document'
, 'LastName' AS 'document/field/@name'
, LastName AS 'document/field'
, '' AS 'document'
FROM @Person
FOR XML PATH(''), ROOT('documents')

关于sql - Sql Server 中的 XML 专用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15266232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com