关于 xpath 元素的 MS TSQL for xml 路径问题
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,我认为您无法使用 T-SQL 来实现这一目标。
您可以获得的最接近的是:
如果您将查询调整为:
抱歉,我无法在这里提供更多帮助 - 猜测这是微软尚未真正考虑的功能(到目前为止)! :-)
马克
I don't think you'll be able to achieve this with T-SQL, unfortunately.
The closest you could get is this:
if you adapt your query to be:
Sorry I can't be of more help here - guess that's a feature Microsoft hasn't really considered (so far)! :-)
Marc
如果您对 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.
如果我理解正确的话 - 你想要一个根元素而不是有许多主根。
使用:
If I understood you correctly - you want one root element instead of having many main roots.
Use: