如何在 XML 中向我的根添加常量? [英] How can I add constants to my root in XML?

查看:31
本文介绍了如何在 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 中向我的根添加常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆