1

私は実際にこれは私がサーバーにtabled-valueを渡して、マルチ検索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. 

を渡したいテーブルの引数である以下の説明

を達成したいが、私はenoughtクリアするのですか?

xml以外の方法でやっても構いません。 私が必要とするのは、2つの列の値の配列を持つ値を選択することです。

+0

Items.Item - XMLまたはテキストのデータタイプは何ですか? –

+0

col1 varchar(32)、col2 int。 – Shimmy

+0

あなたの試みを見せてください.... –

答えて

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()に構文がある可能性があります。

関連する問題