1

其實我是想達到以下描述如何通過向表單值傳遞多重搜索SPROC/UDF?

這是表參數我要傳遞給服務器

<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的方式來執行此操作。 我需要的是選擇加上兩列值的數組的值。

+0

什麼是Items.Item的數據類型 - XML或文本? – 2009-07-26 04:38:32

回答

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 
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()處。