당신은 OUTER APPLY를 사용하고 XML 필드의 부품을 분해 할 수
여기에 xquery의 예입니다.
value() 메서드는 XQuery 표현식을 필요로합니다. 예를 들어
:
DECLARE @T TABLE (Id int identity(1,1) primary key, XmlCol1 xml);
insert into @T (XmlCol1) values
('<auditElement><field id="2881159" type="5" name="Responsiveness" formatstring=""><unSetChoice>2881167</unSetChoice><setChoice>2881166</setChoice></field></auditElement>'),
('<auditElement><field id="2881160" type="6" name="Responsiveness" ><unSetChoice>2881187</unSetChoice><setChoice>2881188</setChoice></field></auditElement>');
select *
from (
select
Id,
a.field.value('@id', 'int') as xmlid,
a.field.value('(unSetChoice)[1]', 'int') as unSetChoice,
a.field.value('(setChoice)[1]', 'int') as setChoice,
a.field.value('@type', 'int') as type,
a.field.value('@name', 'varchar(max)') as name,
a.field.value('@formatstring', 'varchar(max)') as formatstring
from @T t
outer apply t.XmlCol1.nodes('/auditElement/field') as a(field)
where a.field.value('@id', 'int') > 0 and Id > 0
) q
where name = 'Responsiveness';
결과 : 내가 준 샘플의 여러 행을 저장하는 테이블을 가지고 어떤 경우
Id xmlid unSetChoice setChoice type name formatstring
1 2881159 2881167 2881166 5 Responsiveness
2 2881160 2881187 2881188 6 Responsiveness NULL
. 예 : xml 데이터의 행이 10 개있는 경우 선언문을 읽고 출력하는 방법은 무엇입니까? – Jeff