关于 xpath 元素的 MS TSQL for xml 路径问题

发布于 2024-07-14 18:11:29 字数 1346 浏览 4 评论 0原文

我有以下 TSQL 语句:

select 
  tblName     "TblName",
  structure   "TblName/STRUCTURE",
  sqlRetrieve "TblName/SQLRETRIEVE",
  Identifier  "TblName/IDENTIFIER",
  '2'         "TblName/OBJECTTYPE"
from 
  configTable 
for xml path ('')

输出:

<TblName>PD_CODE_PRODUCTS
  <STRUCTURE>PD_CODE_PRODUCTS</STRUCTURE>
  <SQLRETRIEVE>BATCHSP</SQLRETRIEVE>
  <IDENTIFIER>DATA_OWNER</IDENTIFIER>
  <OBJECTTYPE>2</OBJECTTYPE>
</TblName>
<TblName>PD_two
  <STRUCTURE>PD_CODE_PRODUCTS</STRUCTURE>
  <SQLRETRIEVE>BATCHSP</SQLRETRIEVE>
  <IDENTIFIER>DATA_OWNER</IDENTIFIER>
  <OBJECTTYPE>2</OBJECTTYPE>
</TblName>

但我想要此输出(元素名称:

<PD_CODE_PRODUCTS>
  <STRUCTURE>PD_CODE_PRODUCTS</STRUCTURE>
  <SQLRETRIEVE>BATCHSP</SQLRETRIEVE>
  <IDENTIFIER>DATA_OWNER</IDENTIFIER>
  <OBJECTTYPE>2</OBJECTTYPE>
</PD_CODE_PRODUCTS>
<PD_two>
  <STRUCTURE>PD_CODE_PRODUCTS</STRUCTURE>
  <SQLRETRIEVE>BATCHSP</SQLRETRIEVE>
  <IDENTIFIER>DATA_OWNER</IDENTIFIER>
  <OBJECTTYPE>2</OBJECTTYPE>
</PD_two>

有人知道如何使用 T-SQL 实现此目的吗?

谢谢
丹尼尔

I have the following TSQL statement:

select 
  tblName     "TblName",
  structure   "TblName/STRUCTURE",
  sqlRetrieve "TblName/SQLRETRIEVE",
  Identifier  "TblName/IDENTIFIER",
  '2'         "TblName/OBJECTTYPE"
from 
  configTable 
for xml path ('')

which outputs:

<TblName>PD_CODE_PRODUCTS
  <STRUCTURE>PD_CODE_PRODUCTS</STRUCTURE>
  <SQLRETRIEVE>BATCHSP</SQLRETRIEVE>
  <IDENTIFIER>DATA_OWNER</IDENTIFIER>
  <OBJECTTYPE>2</OBJECTTYPE>
</TblName>
<TblName>PD_two
  <STRUCTURE>PD_CODE_PRODUCTS</STRUCTURE>
  <SQLRETRIEVE>BATCHSP</SQLRETRIEVE>
  <IDENTIFIER>DATA_OWNER</IDENTIFIER>
  <OBJECTTYPE>2</OBJECTTYPE>
</TblName>

but I want to have this output (the element name :

<PD_CODE_PRODUCTS>
  <STRUCTURE>PD_CODE_PRODUCTS</STRUCTURE>
  <SQLRETRIEVE>BATCHSP</SQLRETRIEVE>
  <IDENTIFIER>DATA_OWNER</IDENTIFIER>
  <OBJECTTYPE>2</OBJECTTYPE>
</PD_CODE_PRODUCTS>
<PD_two>
  <STRUCTURE>PD_CODE_PRODUCTS</STRUCTURE>
  <SQLRETRIEVE>BATCHSP</SQLRETRIEVE>
  <IDENTIFIER>DATA_OWNER</IDENTIFIER>
  <OBJECTTYPE>2</OBJECTTYPE>
</PD_two>

Does anybody know how to achieve this with T-SQL?

Thanks
Daniel

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

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

发布评论

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

评论(3

夜清冷一曲。 2024-07-21 18:11:29

不幸的是,我认为您无法使用 T-SQL 来实现这一目标。

您可以获得的最接近的是:

<TABLE Name="PD_CODE_PRODUCTS">
  <TblName>
    <STRUCTURE>PD_CODE_PRODUCTS</STRUCTURE>
    <SQLRETRIEVE>BATCHSP</SQLRETRIEVE>
    <IDENTIFIER>DATA_OWNER</IDENTIFIER>
    <OBJECTTYPE>2</OBJECTTYPE>
  </TblName>
</TABLE>
<TABLE Name="PD two">
  <TblName>
    <STRUCTURE>PD_CODE_PRODUCTS</STRUCTURE>
    <SQLRETRIEVE>BATCHSP</SQLRETRIEVE>
    <IDENTIFIER>DATA_OWNER</IDENTIFIER>
    <OBJECTTYPE>2</OBJECTTYPE>
  </TblName>
</TABLE>

如果您将查询调整为:

select 
  tblName '@Name',
  structure   "TblName/STRUCTURE",
  sqlRetrieve "TblName/SQLRETRIEVE",
  Identifier  "TblName/IDENTIFIER",
  '2'         "TblName/OBJECTTYPE"
from 
  configTable 
for xml path ('TABLE')

抱歉,我无法在这里提供更多帮助 - 猜测这是微软尚未真正考虑的功能(到目前为止)! :-)

马克

I don't think you'll be able to achieve this with T-SQL, unfortunately.

The closest you could get is this:

<TABLE Name="PD_CODE_PRODUCTS">
  <TblName>
    <STRUCTURE>PD_CODE_PRODUCTS</STRUCTURE>
    <SQLRETRIEVE>BATCHSP</SQLRETRIEVE>
    <IDENTIFIER>DATA_OWNER</IDENTIFIER>
    <OBJECTTYPE>2</OBJECTTYPE>
  </TblName>
</TABLE>
<TABLE Name="PD two">
  <TblName>
    <STRUCTURE>PD_CODE_PRODUCTS</STRUCTURE>
    <SQLRETRIEVE>BATCHSP</SQLRETRIEVE>
    <IDENTIFIER>DATA_OWNER</IDENTIFIER>
    <OBJECTTYPE>2</OBJECTTYPE>
  </TblName>
</TABLE>

if you adapt your query to be:

select 
  tblName '@Name',
  structure   "TblName/STRUCTURE",
  sqlRetrieve "TblName/SQLRETRIEVE",
  Identifier  "TblName/IDENTIFIER",
  '2'         "TblName/OBJECTTYPE"
from 
  configTable 
for xml path ('TABLE')

Sorry I can't be of more help here - guess that's a feature Microsoft hasn't really considered (so far)! :-)

Marc

梦回梦里 2024-07-21 18:11:29

如果您对 XML 结构有任何控制权,我恳求您不要做您想做的事情。 我遇到过 XML 文档,其中 XML 元素名称根据数据而变化,使用起来很糟糕。 XPath 语句很难,XSLT 几乎不可能。

按照 marc_s 的建议去做,稍后你会感谢他的。

如果你无法控制结构,那么我对你表示同情。

If you have any control over the XML structure I beg you not to do what you are trying to do. I've run into XML documents where the XML element names change based on data and it is horrible to work with. XPath statements are hard, XSLT is almost impossible.

Do what marc_s suggests, you'll thank him later.

If you have no control over the structure then you have my sympathies.

云胡 2024-07-21 18:11:29

如果我理解正确的话 - 你想要一个根元素而不是有许多主根。

使用:

select   tblName '@Name',  structure   "TblName/STRUCTURE",  sqlRetrieve "TblName/SQLRETRIEVE",  Identifier  "TblName/IDENTIFIER",  '2'         "TblName/OBJECTTYPE"from   configTable for xml path (''), root('TABLE'), type

If I understood you correctly - you want one root element instead of having many main roots.

Use:

select   tblName '@Name',  structure   "TblName/STRUCTURE",  sqlRetrieve "TblName/SQLRETRIEVE",  Identifier  "TblName/IDENTIFIER",  '2'         "TblName/OBJECTTYPE"from   configTable for xml path (''), root('TABLE'), type
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文