简体   繁体   English

在T-SQL中创建具有多个属性的XML

[英]creating XML in T-SQL with multiple attributes

Would like to ask how to create xml doc in T SQL which looks like this. 想问一下如何在T SQL中创建xml doc,如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<root firstAttribute="test" secondAttribute="test" etc...>
   <order firstAttribute="test" secondAttribute="test" etc...></order>
</root>

I don't really know how to insert multiple argumets. 我真的不知道如何插入多个argumets。

Thanks 谢谢

use **FOR XML to Convert the table to the XML format ** 使用** FOR XML将表转换为XML格式**

SELECT TOP 10 *
FROM Customers 
inner join Orders  on Customers.CustomerID= Orders.CustomerID 
FOR XML AUTO  

and add string before 并在前面添加字符串

select '<?xml version="1.0" encoding="UTF-8"?>'  + (
SELECT TOP 10 *
FROM Customers 
inner join Orders  on Customers.CustomerID= Orders.CustomerID 
FOR XML AUTO  
) 

This will provide you with the logic you need to create xml from a table "TestTable". 这将为您提供从表“ TestTable”创建xml所需的逻辑。

DECLARE @TestTable table (firstAttribute int, secondAttribute varchar(100))

INSERT INTO @TestTable values (1,'Test 1')
INSERT INTO @TestTable values (2,'Test 2')


SELECT '<?xml version="1.0" encoding="UTF-8"?>'  + (
    SELECT 
           firstAttribute,
           secondAttribute     
    FROM   @TestTable
    FOR XML PATH ('root')

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM