使用 TSQL 变量 + X查询

发布于 2024-09-18 21:22:25 字数 542 浏览 4 评论 0原文

所有,

我有以下代码:

--xml is already declared and populated with an xml document

SELECT
ent.query('TradeId').value('.','VARCHAR(10)') TradeId
FROM                 
@xml.nodes('//table/trade[1]') Col(ent)  

如何将表达式 //table/trade[1] 作为变量,以便我可以在循环中增加 [1] ?例如,我想要像下面这样不起作用的东西:

DECLARE @KLM varchar(100)
SET @KLM= '//table/trade[1]'
SELECT
ent.query('TradeId').value('.','VARCHAR(10)') TradeId
FROM                 
@xml.nodes(@KLM) Col(ent) 

有什么想法吗?

谢谢,

All,

I have the following code:

--xml is already declared and populated with an xml document

SELECT
ent.query('TradeId').value('.','VARCHAR(10)') TradeId
FROM                 
@xml.nodes('//table/trade[1]') Col(ent)  

How can I have the expression //table/trade[1] as a variable so I can increase the [1] in a loop ? For example I would like something like the below which is not working:

DECLARE @KLM varchar(100)
SET @KLM= '//table/trade[1]'
SELECT
ent.query('TradeId').value('.','VARCHAR(10)') TradeId
FROM                 
@xml.nodes(@KLM) Col(ent) 

Any ideas?

Thanks,

M

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

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

发布评论

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

评论(1

岁月染过的梦 2024-09-25 21:22:28

如果从 .nodes() 方法中删除位置引用,您应该获得包含所有 TradeId 值的结果集。

SELECT 
ent.query('TradeID').value('.','VARCHAR(10)') TradeId 
FROM                  
@xml.nodes('//table/trade') Col(ent)

然后,您应该能够迭代该结果集,尽管我想添加关于“关系模型中的迭代通常是一个坏主意”的标准警告。

这有帮助吗?如果没有,这个故事还有更多内容吗?

If you remove the positional reference from your .nodes() method, you should get a resultset containing all of the TradeId values.

SELECT 
ent.query('TradeID').value('.','VARCHAR(10)') TradeId 
FROM                  
@xml.nodes('//table/trade') Col(ent)

You should then be able to iterate through that resultset, although I would like to add the standard caveat about "iteration in a relational model is generally a bad idea".

Does that help? If not, is there more to the story?

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