How can I add constants to my root in XML?(如何在 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>
这篇关于如何在 XML 中向我的根添加常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 XML 中向我的根添加常量?
基础教程推荐
- 在 SQL 中连接多个表 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 无法解决整理冲突 2021-01-01
