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. 

내가 분명히 한 정도로 내가 서버에 전달하려는 테이블 인수 오전를 달성하려면?

xml과 다른 방식으로 사용해도 상관 없습니다. 필요한 것은 두 개의 열 값 중 하나의 값을 선택하는 것입니다.

+0

XML 또는 텍스트 인 Items.Item의 데이터 유형은 무엇입니까? –

+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; 

BTW 메모리에서이 작업을 수행하는 경우 구문을 사용하지 않을 수 있습니다 (특히 text()).