从 Sql Server 2005 检索 Xml 输出

发布于 2024-10-28 12:02:43 字数 848 浏览 1 评论 0原文

大家好,有人可以为我提供一些线索或解决方案来检索记录集,如下所示

注意:我阅读了 MSDN 文档,但除了脱发之外没有任何结果:(

只是假设我有 2 个通过 Rid 连接的表字段

表 1 字段、

Rid,UserName,Hash

表 2 字段

Rid,Phone,City,Email

table1table2 通过 Rid 字段连接

。使用 xml auto、xml 显式或您在 SQL Server Express 2005 中获得的任何 xml 操作的 xml 输出。

预期输出:

<UserDetails>
    <Account>
        <UserName></UserName>
        <Hash></Hash>
    </Account>
    <Personal>
        <Phone1></Phone1>
        <City1></City1>
        <Phone2></Phone2>
        <City2></City2>
    </Personal>
</UserDetails>

个人详细信息可以有 n 条记录,但帐户详细信息只有一条。

Hi could someone provide me some clues or solution to retrieve record sets like below

Note: I read the MSDN documentation but leads me nowhere but hair loss :(

Just for the assumption think I have 2 tables connected via Rid field

Table 1 fields,

Rid,UserName,Hash

Table2 fields

Rid,Phone,City,Email

table1 and table2 are connected via the Rid field.

I would like to have a xml output using xml auto, or xml explicit or which ever xml operations you got in SQL Server Express 2005.

Output expected:

<UserDetails>
    <Account>
        <UserName></UserName>
        <Hash></Hash>
    </Account>
    <Personal>
        <Phone1></Phone1>
        <City1></City1>
        <Phone2></Phone2>
        <City2></City2>
    </Personal>
</UserDetails>

The personal details can have n records. But account details are only one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南…巷孤猫 2024-11-04 12:02:43

你可以尝试这个 - 我不能做的是创建那些“嵌入式枚举”,

<Phone1></Phone1>
<City1></City1>
<Phone2></Phone2>
<City2></City2>

我不知道有什么方法可以轻松地制作那些按顺序编号的 XML 标签......

我所能做的就是我将在end:

SELECT
    t1.UserName AS 'Account/UserName',
    t1.UserHash AS 'Account/Hash',
    (SELECT
        t2.Phone AS 'Phone',
        t2.City AS 'City'
     FROM table2 t2
     WHERE t2.Rid = t1.Rid
     FOR XML PATH(''), TYPE
    ) AS 'Personal'
FROM
    table1 t1
FOR XML PATH('UserDetails'), ROOT('Users')

这给了我一个类似的输出:

<Users>
  <UserDetails>
    <Account>
      <UserName>xxxxx</UserName>
      <Hash>hhhhhhh</Hash>
    </Account>
    <Personal>
      <Phone>.....</Phone>
      <City>.....</City>
      <Phone>..........</Phone>
      <City>.........</City>
    </Personal>
  </UserDetails>
  <UserDetails>
    <Account>
      <UserName>cccccccccccc</UserName>
      <Hash>hhhhhhhhhhhh</Hash>
    </Account>
    <Personal>
      <Phone>................</Phone>
      <City>...........</City>
    </Personal>
  </UserDetails>
</Users>

当然,如果你愿意,你可以添加一个 WHERE t1.rid = xxx 条件到你的外部 SELECT 以限制检索到的用户及其详细信息。

You could try this - what I cannot do is create those "embedded enumerations"

<Phone1></Phone1>
<City1></City1>
<Phone2></Phone2>
<City2></City2>

I don't know of any way to easily make those sequentially numbered XML tags ...

All I can do is the output I'm going to show at the end:

SELECT
    t1.UserName AS 'Account/UserName',
    t1.UserHash AS 'Account/Hash',
    (SELECT
        t2.Phone AS 'Phone',
        t2.City AS 'City'
     FROM table2 t2
     WHERE t2.Rid = t1.Rid
     FOR XML PATH(''), TYPE
    ) AS 'Personal'
FROM
    table1 t1
FOR XML PATH('UserDetails'), ROOT('Users')

This gives me an output something like:

<Users>
  <UserDetails>
    <Account>
      <UserName>xxxxx</UserName>
      <Hash>hhhhhhh</Hash>
    </Account>
    <Personal>
      <Phone>.....</Phone>
      <City>.....</City>
      <Phone>..........</Phone>
      <City>.........</City>
    </Personal>
  </UserDetails>
  <UserDetails>
    <Account>
      <UserName>cccccccccccc</UserName>
      <Hash>hhhhhhhhhhhh</Hash>
    </Account>
    <Personal>
      <Phone>................</Phone>
      <City>...........</City>
    </Personal>
  </UserDetails>
</Users>

Of course, if you want, you can add a WHERE t1.rid = xxx condition to your outer SELECT in order to limit the user and its details retrieved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文