1

我其实是想实现下面的描述

这是我要传递给服务器的表参数

<items>
    <item category="cats">1</item>
    <item category="dogs">2</item>
</items>    

SELECT * FROM Item
    WHERE Item.Category = <one of the items in the XML list> 
    AND Item.ReferenceId = <the corresponding value of that item xml element>

--Or in other words:
SELECT FROM Items
    WHERE Item IN XML according to the splecified columns.

我够清楚吗?

我不介意以与 xml 不同的方式进行操作。我需要的是选择与其两个列值的数组匹配的值。

4

2 回答 2

2

因此,您应该能够解析 XML,并像表格一样加入它。

DECLARE @foo XML;

SET @foo = N'<items>
    <item category="cats">1</item>
    <item category="dogs">2</item>
</items>';

WITH xml2Table AS
(
SELECT
    x.item.value('@category', 'varchar(100)') AS category,
    x.item.value('(.)[1]', 'int') AS ReferenceId
FROM
    @foo.nodes('//items/item') x(item)
)
SELECT
    * 
FROM
    Item i
    JOIN
    xml2Table_xml x ON i.category = x.Category AND i.ReferenceId = x.ReferenceId
于 2009-07-26T09:23:47.113 回答
0
DECLARE @x XML;
SELECt @x = N'<items>
    <item category="cats">1</item>
    <item category="dogs">2</item>
</items>';

WITH shred_xml AS (
  SELECT x.value('@category', 'varchar(100)') AS category,
    x.value('text()', 'int') AS ReferenceId
  FROM @x.nodes('//items/item') t(x) )
SELECT * 
  FROM Item i
  JOIN shred_xml s ON i.category = s.category 
    AND i.ReferenceId = s.ReferenceId; 

顺便说一句,从内存中执行此操作,可能会删除一些语法,特别是在 text() 处。

于 2009-07-26T07:17:07.433 回答