1

我将以下 xml 发送到要在 where 子句中使用的 sql server 存储过程。

<root>
 <id>1</id>
 <id>2</id>
 <id>3</id>
</root

我正在尝试找到一种方法来使用 select 语句,其中 where 子句将检查传递的 xml 参数中的任何 id 值。

像这样的东西:

@xml xml
select * from table
where (if exists (select id in @xml))

对不起,我不太擅长 sql。

4

2 回答 2

1
declare @xml xml;
set @xml = CAST('<root><id>1</id><id>2</id><id>3</id></root>' AS XML)

select id from mytable
where exists(select 1 from @xml.nodes('/root/id')as result(node)
             where node.value('(.)[1]', 'int') = id)
于 2013-07-03T02:03:42.463 回答
0

我总是参考Erland Sommarskog 的这个链接来处理 sql 中的数组。它提供了一个很好的示例,并将其与将数组传递给存储过程的其他机制进行了对比。

请注意,如果您使用的是 SQL Server 2008,他有另一个链接详细说明了表值参数的使用。对于您的示例而言,这可能有点矫枉过正,但如果您热衷于了解可用的内容,那么它都值得一读。

于 2013-07-03T02:33:02.570 回答