如何在 XML 中向我的根添加常量?
这是我生成 XML 的查询:
This is my query that generates an XML:
SELECT [a] a
,[b] b
,[c] c
,[d] d
,[e] e
,[f] f
,[g] g
FROM test
ORDER BY 1
FOR XML PATH('a'), ROOT('ROOT'), ELEMENTS XSINIL
生成的 XML 以这个根开始:
The XML generated starts with this root:
我的目标是拥有一个具有更多属性的根
My goal is to have a root with more attributes
这个属性就像我想附加到我的根的常量(它们不是来自 select 的列).它们将被修复,无论选择什么
This attributes are like constants (thet are not columns from select) that I want to append to my root. They will be fixed, whatever will be the select
可以吗?
推荐答案
这是一个概念性示例.请试一试.
Here is a conceptual example. Please give it a shot.
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, city VARCHAR(30));
INSERT INTO @tbl (city) VALUES
('Miami'),
('Orlando');
-- DDL and sample data population, end
SELECT 'SIN_OPE' AS [@cod_1], '08' AS [@cod_2], '12' AS [@num_reg]
, 'yyyyyyyyyyyyyyyyyyy.xsd' AS [@xsi:noNamespaceSchemaLocation]
, (
SELECT *
FROM @tbl
FOR XML PATH('r'), TYPE
)
FOR XML PATH('root'), TYPE, ELEMENTS XSINIL;
输出
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" cod_1="SIN_OPE"
cod_2="08" num_reg="12"
xsi:noNamespaceSchemaLocation="yyyyyyyyyyyyyyyyyyy.xsd">
<r>
<ID>1</ID>
<city>Miami</city>
</r>
<r>
<ID>2</ID>
<city>Orlando</city>
</r>
</root>
相关文章